我有一个 Android 应用程序,它正在接收推送消息。
如果用户单击推送消息,则会调用 Activity GoToNotification。 到目前为止,这是有效的。
但是如何获取推送消息的内容呢? Bundle.extra? 我正在尝试对推送消息的某些内容使用react,但 bundle 额外内容始终为空。
这是我到目前为止的代码:
private void sendNotification(String msg, Bundle extras) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, GoToNotification.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("Warning")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setExtras(extras)
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
public class GoToNotification extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = this.getIntent().getExtras();
if (extras != null) {
System.out.println(extras.toString());
} else {
System.out.println("extras is null");
}
}
}
最佳答案
Intent notificationIntent = new Intent(context, SampleActivity.class);
notificationIntent.putExtra("EXTRA_ID", "ur data");
在你的 Activity 中
if (extras != null) {
ID = extras.getString("EXTRA_ID");
}
关于java - 点击推送消息后访问推送通知内容(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33166842/