如何处理不同的GCM通知点击

时间:2016-03-17 11:38:47

标签: android android-intent google-cloud-messaging

我在我的应用中使用了GCM推送通知。一切都好。

当我收到通知时,当我点击通知时,我可以移动其他活动。我正在将url链接传递给另一个活动。但是,如何处理点击多个通知。

这里说当我收到5个通知并点击任何通知我正在转移到其他活动,但我传递的链接是第一个通知网址。我使用不同的通知ID

 NotificationManager notificationManager = 

(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

 notificationManager.notify(x
                    /*ID of notification */
                , notificationBuilder.build());

2 个答案:

答案 0 :(得分:0)

在创建PendingIntent时使用PendingIntent.FLAG_UPDATE_CURRENT。

在将意图传递给PendingIntent之前,每次都将其动作设置为不同。 e.g: -

intent.setAction(Long.toString(System.currentTimeMillis()));

答案 1 :(得分:0)

请参阅以下代码以处理具有不同数据处理的多个通知:

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

在上面的代码我也处理通知删除意图,看到每个通知都有不同的通知ID与PendingIntent.FLAG_CANCEL_CURRENT标志。