0

Here is my code, When my app is running in foreground it opens targeted activity but when app is closed or in background, it doesn't opens targeted activity, please help me to solve this problem

I want that by clicking on notification will open targeted activity even app is running / closed.

 public void onMessageReceived(RemoteMessage remoteMessage) {

    session = new Session(this);
  //  if (session.getBscloggedin() || session.getAdploggedin()) {
       // Log.d(TAG, "FROM:" + remoteMessage.getFrom());

        //Check if the message contains data
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data: " + remoteMessage.getData());
        }

        //Check if the message contains notification

        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Mesage body:" + remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody());
        }
    }


/**
 * Dispay the notification
 * @param body
 */


private void sendNotification(String body) {

    //for CS101 announcement notification
    if (body.contains("CS101")) {
        Intent intent = new Intent(this, CS101noti.class);
        intent.putExtra("body", body);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, 0);
        //Set sound of notifica tion
        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("mine")
                .setContentText(body)
                .setAutoCancel(true)
                .setSound(notificationSound)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0 /*ID of notification*/, notifiBuilder.build());
    }
KENdi
  • 7,576
  • 2
  • 16
  • 31

4 Answers4

1

Request code should be unique.

Generally people share with us example like giving request code 0(zero). I am the one of copy/paste developer sometimes. But recently I faced with error using this line.

PendingIntent.getActivity(myContext, 0, myIntent, 0);

Whenever app receives notification, intent activity receives old data because of requestCode. Here is the documentation about that method.

Returns an existing or new PendingIntent matching the given parameters.

So second parameter(requestCode) should be unique to intent. If you use same request code for every notification, activity will be receive old data. So your requestCode should be unique.

There is also another solution.Use PendingIntent.FLAG_UPDATE_CURRENT as flag. The doc says that;

If the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.

mertsimsek
  • 266
  • 2
  • 10
0

use "click_action" attribute from your backend from which you are sending push notifications. in that attribute value, you have to pass the fully qualified class path of activity, which should be opened when you click on that notification.for more reference refer handling firebase push

Hardik Mehta
  • 867
  • 1
  • 12
  • 20
0
Intent intent = new Intent(this, CS101noti.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
....
Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
0

onMessageReceived callback is not called when you receive notification messages in background. If you receive this type of message in background, you can only handle that in a launcher activity. enter image description here

The solution would be to either change the backend so that it sends only data messages and you handle them internally, or write some routing code inside your launcher activity.

satorikomeiji
  • 469
  • 6
  • 16