使用挂起意图的 putExtra 不起作用

问题描述 投票:0回答:4

我在 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 是正确的,即我总是得到更新的 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
4个回答
43
投票

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

为了避免这种情况,请在调用 PendingIntent.FLAG_CANCEL_CURRENT

 时使用标志 
PendingIntent.getActivity()
 来实际获取新的:

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

或者,如果您只想更新附加功能,请使用标志

PendingIntent.FLAG_UPDATE_CURRENT


12
投票

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

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

5
投票

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

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

    handleIntentExtraFromNotification(intent);
}

0
投票

当我使用待处理意图推送通知时,我使用了代码:

//current activity
val intent = Intent(this, TargetActivity::class.java)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    intent.putExtra("value", value)
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)

//TargetActivity
val fetchedValue = intent.getStringExtra("value").toString()

但是通过在 TargetActivity fetchedValue 中使用此代码=

这里我更新代码并获取fetchedValue

//current activity
val intent = Intent(this, TargetActivity::class.java)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    intent.putExtra("value", value)
val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        PendingIntent.getActivity(
            this,
            0,
            intent,
            PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
        )
    } else {
        PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    }

//TargetActivity
val fetchedValue = intent.getStringExtra("value").toString()

此代码可防止应用程序崩溃。

© www.soinside.com 2019 - 2024. All rights reserved.