如何将通知中的额外字符串传递给活动

时间:2015-11-26 15:30:14

标签: android android-intent android-activity

我正在尝试将推送通知的值传递给我的活动。但它总是返回null。

以下是我尝试传递额外字符串的方法:

Intent notificationIntent = new Intent(context, Register.class);
notificationIntent.putExtra("NotificationMessage", new_key.toString());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;

// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);      

这是我的活动,我试图获得这个额外的字符串。

Intent intent = getIntent();
String msg = intent.getStringExtra("NotificationMessage");
Toast.makeText(this, "Hello"+msg, Toast.LENGTH_LONG).show();

这个烤面包总是显示“Hello null”。有人可以帮忙吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

通过PendingIntent.setContentIntent()添加到您的通知中。单击通知时会触发意图。

Notification notification = new NotificationCompat.Builder(context)
        ...
        .setContentIntent(intent)
        ...
        .build();