Search code examples
androidnotificationsbroadcastreceivergoogle-cloud-messagingandroid-pendingintent

Subsequent(ie. AFTER FIrst) notification pendingintent not launching activity when app is initailly closed


Scenario 1 - App is open, receive pendingintent in notification and when notification is clicked,

opens activity with new content, every pendingintent notification received after the first works in a similar fashion

.

Scenario 2 - App is closed (not running), receive pendingintent in notification and when notification is clicked,

opens activity with new content, every pendingintent notification received after the first does not work in a similar fashion (doesn't launch activity)

.

Code of Pending Intent: Intent nIntent = new Intent(getApplication(), ChatActivity.class);

    nIntent.putExtra("chattingFrom", chattingToName);
    nIntent.putExtra("chattingToName", chattingFrom);
    nIntent.putExtra("chattingToDeviceID", chattingFromDeviceID);
    nIntent.putExtra("chattingFromDeviceID", chattingToDeviceID);

    NOTIFICATION_ID = NOTIFICATION_ID + 1;

    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, nIntent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);

    Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notify);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Chat App")
            .setStyle(new NotificationCompat.BigTextStyle().bigText("New message from " + chattingFrom + ": " + msg))
            .setContentText("New message from " + chattingFrom + ": " + msg)
            .setAutoCancel(true)
            .setTicker("New message from " + chattingFrom)
            .setSound(sound);

    mBuilder.setContentIntent(contentIntent);

    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

Main Problem: When user clicks notification (app is closed/not running), activity opens with new content(first click), every notification after that does not work(subsequent clicks).

Everything works when app is open, and then notification comes in.


Solution

  • I added a dummy action to my intent, see below:

    For example nIntent.setAction("foo")