我正在从一个应用程序向另一个应用程序发送通知:这是我的用于向设备令牌发送通知的代码。
private void sendNotification(final String NotificationBody, int p) {
final FormModel model=feedbacks.get(p);
final String ServerKey="key";// hiding the real key
new AsyncTask<Void,Void,Void>(){
@Override
protected Void doInBackground(Void... params) {
try {
OkHttpClient client = new OkHttpClient();
JSONObject json=new JSONObject();
JSONObject dataJson=new JSONObject();
dataJson.put("body",NotificationBody);
dataJson.put("title","Admin's Response to your
FeedBack, Click to View");
dataJson.put("sound", "default");
dataJson.put("vibrate", "default");
json.put("notification",dataJson);
json.put("to",model.getTopic());
RequestBody body = RequestBody.create(JSON,
json.toString());
Request request = new Request.Builder()
.header("Authorization","key="+ServerKey)
.url("https://fcm.googleapis.com/fcm/send")
.post(body)
.build();
Response response = client.newCall(request).execute();
String finalResponse = response.body().string();
}catch (Exception e){
//Log.d(TAG,e+"");
}
return null; }
}.execute();
}
我收到通知,但是当我单击通知时,它没有打开所需的活动,而只是打开了应用程序。这是我的接收通知的代码。
Intent intent = new Intent(this, AdminResponse.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri=
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new
NotificationCompat.Builder(MyFirebaseMessagingService.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt() ,
notificationBuilder.build());