【问题标题】:putExtra using pending intent not workingputExtra 使用挂起的意图不起作用
【发布时间】:2013-04-28 21:41:27
【问题描述】:

我在我的 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。有人可以指出问题吗?是因为未决意图吗?如果是,那我该如何处理呢?

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 很棒的答案!谢谢。
    • 如果不清楚:PendingIntent pi = PendingIntent.getActivity(this,0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    【解决方案2】:

    正如 Joffrey 所说,PendingIntent 与您提供的第一个 Intent 一起重用。您可以尝试使用标志 PendingIntent.FLAG_UPDATE_CURRENT。

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

    【讨论】:

      【解决方案3】:

      也许您仍在使用旧的意图。试试这个:

      @Override
      protected void onNewIntent(Intent intent) {
          super.onNewIntent(intent);
          //try using this intent
      
          handleIntentExtraFromNotification(intent);
      }
      

      【讨论】:

      • 非常感谢。我一直在与这个作斗争
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多