push-notification - putExtra 使用挂起意图不起作用

标签 push-notification android-notifications android-notification-bar android-pendingintent

我在 GCMIntentservice 中编写了一段代码,用于向许多用户发送推送通知。我使用的NotificationManager将在单击通知时调用DescriptionActivity类。我还将 GCMIntentService 中的 event_id 发送到 DescriptionActivity

protected void onMessage(Context ctx, Intent intent) {
     message = intent.getStringExtra("message");
     String tempmsg=message;
     if(message.contains("You"))
     {
        String temparray[]=tempmsg.split("=");
        event_id=temparray[1];
     }
    nm= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    intent = new Intent(this, DescriptionActivity.class);
    Log.i("the event id in the service is",event_id+"");
    intent.putExtra("event_id", event_id);
    intent.putExtra("gcmevent",true);
    PendingIntent pi = PendingIntent.getActivity(this,0, intent, 0);
    String title="Event Notifier";
    Notification n = new Notification(R.drawable.defaultimage,message,System.currentTimeMillis());
    n.setLatestEventInfo(this, title, message, pi);
    n.defaults= Notification.DEFAULT_ALL;
    nm.notify(uniqueID,n);
    sendGCMIntent(ctx, message);

}

这里我在上述方法中获得的 event_id 是正确的,即我总是得到更新的。但在下面的代码(DescriptionActivity.java)中:

    intent = getIntent();
    final Bundle b = intent.getExtras();
    event_id = Integer.parseInt(b.getString("event_id"));

此处的 event_id 始终为“5”。无论我在 GCMIntentService 类中放置什么,我得到的 event_id 始终是 5。有人可以指出问题吗?是因为未决意图吗?如果是的话我该如何处理?

最佳答案

PendingIntent 与您提供的第一个 Intent 一起重用,这是您的问题。

为避免这种情况,请使用标志 PendingIntent.FLAG_CANCEL_CURRENT当您调用 PendingIntent.getActivity() 来实际获取一个新的事件时:

PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

或者,如果您只想更新附加内容,请使用标志 PendingIntent.FLAG_UPDATE_CURRENT

关于push-notification - putExtra 使用挂起意图不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16376643/

相关文章:

android - 通知 : Sony devices do not show any notification

ios - 数据变化时解析推送通知

jquery - 如何为跨平台Web应用程序实现推送通知服务?

javascript - 服务工作和推送通知

android - Pusher Beams - 推送通知取消订阅在 android 中不起作用

android - 是否可以减少 Android 中显示的通知时间?

java - Android 通知多个默认值

android - 如何设置微小的通知图标徽章

android - Android 中的音频播放器

android - 如何以编程方式在 Android 中打开/关闭通知?