I am sending notification from one app to another app: here is my code for sending a notification to a a device Token.

  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();
}

I am getting the notification, but when I click on the notification it's not opening the desired activity and just open the app. Here is my code for receiving notification.

     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());

0 Answers