0

I have used GCM Push Notifications into my app. Everything is fine.

When i get notification and when i click notification I can move other activity.I am passing url link to another activity.but,How to handle click on multiple notifications.

Here say when I get 5 notifications and click on any notification I am moving to other activity but the link which I am passing is the first notification url. I am using different notification id

 NotificationManager notificationManager = 

(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

 notificationManager.notify(x
                    /*ID of notification */
                , notificationBuilder.build());
JKennedy
  • 18,150
  • 17
  • 114
  • 198
Sanjeev
  • 292
  • 2
  • 5
  • 24
  • 1
    How is this question different from http://stackoverflow.com/questions/36055555/how-to-handle-gcm-notifications-when-different-gcm-notifications-receive ? – Michael Mar 17 '16 at 11:41
  • can you tell me the solution – Sanjeev Mar 17 '16 at 11:41
  • 1
    No. And you shouldn't post the same question multiple times. See http://stackoverflow.com/help/no-one-answers – Michael Mar 17 '16 at 11:43

2 Answers2

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
  • where should i give this in pending intent? PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */ , intent, PendingIntent.FLAG_ONE_SHOT); – Sanjeev Mar 17 '16 at 11:45
  • let me try please help i am stuck from morning – Sanjeev Mar 17 '16 at 11:47
  • int FLAG_UPDATE_CURRENT Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. – maveroid Mar 17 '16 at 11:48
  • make sure to use your URL as extra data to the intent passed in PendingIntent – maveroid Mar 17 '16 at 11:49
  • yes i have url link as extras i can see i am getting different url , when i receive notification but when i tap the notification i am able to move other activity but with old url link – Sanjeev Mar 17 '16 at 11:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106587/discussion-between-sanjeev-and-maveroid). – Sanjeev Mar 17 '16 at 11:51
0

See below code to handle multiple notifications with different data handling:

private void sendNotification(Bundle extras) 
{
    String detail = extras.getString("detail");
    String title = extras.getString("title");
    if((title==null || title.trim().length()==0) && (detail==null || detail.trim().length()==0))
    {
        //title=getString(R.string.app_name);
        return;
    }
    NOTIFICATION_ID = (int) (System.currentTimeMillis() / 1000L);

    Intent notificationIntent = new Intent(this, ContainerActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    String data = extras.getString("data");
    notificationIntent.putExtra("data",data);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
    bigTextStyle = bigTextStyle.bigText(detail);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setContentTitle(title)
    .setContentText(detail)
    .setContentIntent(pendingIntent)
    .setSmallIcon(R.drawable.app_logo)
    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.app_logo))
    .setWhen(System.currentTimeMillis())
    .setAutoCancel(true)
    .setVibrate(new long[] { 0, 100, 200, 300 })
    .setPriority(NotificationCompat.PRIORITY_MAX)
    .setStyle(bigTextStyle);

    mBuilder.setDeleteIntent(getDeleteIntent(NOTIFICATION_ID));

    Set<String> pnIdsSet = ((MyAccountApplication)getApplication()).getPrefs().getStringSet("PUSH_IDS", null);
    if(pnIdsSet==null)
    {
        pnIdsSet=new HashSet<String>();
    }
    pnIdsSet.add(""+NOTIFICATION_ID);
    ((MyAccountApplication)getApplication()).getPrefs().edit().putStringSet("PUSH_IDS", pnIdsSet).commit();

    mBuilder.setContentIntent(pendingIntent);
    Notification n = mBuilder.build();

    n.flags |= Notification.FLAG_SHOW_LIGHTS;
    n.flags |= Notification.FLAG_AUTO_CANCEL;
    n.defaults = Notification.DEFAULT_ALL;      
    n.when = System.currentTimeMillis();

    mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(NOTIFICATION_ID, n);
}
private PendingIntent getDeleteIntent(int pnId)
{
    Intent intent = new Intent(this, NotificationBroadcastReceiver.class);
    intent.setAction("notification_cancelled");
    intent.putExtra("PN_ID", ""+pnId);
    return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}

in above code I am handling notification delete intent too, see each notification have different notification id with PendingIntent.FLAG_CANCEL_CURRENT flag.

MohdTausif
  • 498
  • 4
  • 13