0

I am using GCM Notification in my app.I parse the data in "onMessageReceived" method where i get some unique "URL" corresponding to GCM Message.When I receive single notification Every thing is fine,But when i receive multiple notifications problem occurs when i tap any notification last received notification url loads.

public void onMessageReceived(String from, Bundle data) {

        String message = data.getString("message");
        String country = data.getString("country");
        String url = data.getString("url");
        String description = data.getString("description");
        String id = data.getString("id");
}

I get url from GCM Notification

Intent intent = new Intent(this, Detailview.class).putExtra("url", url); 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(Mainscreen.class);
stackBuilder.addNextIntent(intent);

PendingIntent pendingIntent= stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_ONE_SHOT );
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image, R.drawable.ic_stat_ic_notification);
contentView.setTextViewText(R.id.title, message);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_stat_ic_notification)
        .setAutoCancel(true)
        .setContent(contentView)
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntent);
NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Integer.parseInt(id)
            /*ID of notification */
        , notificationBuilder.build());

so when i tap the notification i am able to launch only last url How i can solve this problem?

Yusuf K.
  • 4,195
  • 1
  • 33
  • 69
Sanjeev
  • 292
  • 2
  • 5
  • 24
  • use different `NOTIFICATION_ID` for different type of notifications, which will create a separate notification strip in status bar accordingly. – Vipul Asri Mar 17 '16 at 09:18
  • notificationManager.notify(Integer.parseInt(id), notificationBuilder.build()) here i am using different id – Sanjeev Mar 17 '16 at 09:21

2 Answers2

0

Just add time of arrive as a differntiator to each notification

PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
Ram Prakash Bhat
  • 1,308
  • 12
  • 21
  • I just gave this answer on different thread asked by the author. http://stackoverflow.com/questions/36059469/how-to-handle-different-gcm-notifications-clicks – maveroid Mar 17 '16 at 11:50
0

use

PendingIntent.FLAG_UPDATE_CURRENT 

when creating PendingIntent.

Also before passing intent to PendingIntent set its action different everytime. e.g.:-

intent.setAction(Long.toString(System.currentTimeMillis()));
maveroid
  • 1,840
  • 1
  • 20
  • 20