2

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.

Philip Herbert
  • 4,599
  • 2
  • 37
  • 40

2 Answers2

1

I think you should take out PendingIntent.FLAG_ONE_SHOT since this would render the PendingIntent usable only once.

Koh
  • 1,570
  • 1
  • 8
  • 6
1

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

For example nIntent.setAction("foo")

Philip Herbert
  • 4,599
  • 2
  • 37
  • 40
  • http://stackoverflow.com/questions/3168484/pendingintent-works-correctly-for-the-first-notification-but-incorrectly-for-the?rq=1 – Philip Herbert Feb 14 '15 at 02:13