how to handle different GCM notifications clicks

2019-09-05 18:41发布

问题:

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());

回答1:

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()));


回答2:

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.