推送通知打开具有相同意图的活动

时间:2014-07-14 05:17:34

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

我收到数据推送通知(意图)。如果我收到两个或多个具有不同ID的通知,但打开一个Activity和ID是相同的。例如,我收到三个不同id = 1,2,3的通知。但是当Activity启动时,使用一个id = 3.当我点击带有ID 1和2的第一个或第二个通知时,打开id为3的Activity。可以帮助理解我在代码中的错误吗?

NOTIFICATION_ID ++;
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(msg.getString("title"))
        .setContentText(msg.getString("message"))
        .setDefaults(Notification.DEFAULT_SOUND)
        .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(Picasso.with(getApplicationContext()).load(msg.getString("icon")).get()).setSummaryText(msg.getString("message")))        
        .setAutoCancel(true);

        Log.e("msg---",msg.toString());
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName(this, ActivityDetail.class));
//        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra(Keys._PostId,msg.getString("id"));
        intent.putExtra(Keys._Image, msg.getString("icon"));
        intent.putExtra(Keys._PostType,  msg.getString("post_type"));       
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

1 个答案:

答案 0 :(得分:7)

像这样创建你的PendingIntent并做一个技巧

PendingIntent contentIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), intent, PendingIntent.FLAG_UPDATE_CURRENT);