我已经搜索了大约一个小时来找到一些解决方案,当用户点击通知框时如何向活动发送额外内容,但我发现的所有内容都对我不起作用。
我需要将事件 ID 传递给显示有关此事件的用户信息的活动。
我使用了 setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)、setAction("Action")、PendingIntent.FLAG_UPDATE_CURRENT 以及所有这些解决方案的组合,但都没有奏效。
那是我的代码:
Notification.Builder notiBuilder = new Notification.Builder(context);
notiBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.logo));
notiBuilder.setSmallIcon(R.drawable.logo);
notiBuilder.setContentTitle(context.getString(R.string.eventNitificationTitle));
notiBuilder.setContentText(obj.getString("nazwa") + " " + obj.getString("data"));
Intent eventIntenet = new Intent(context, EventActivity.class);
eventIntenet.setAction("Action_"+id);
eventIntenet.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
eventIntenet.putExtra("eventID", id);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, eventIntenet, PendingIntent.FLAG_UPDATE_CURRENT);
eventIntenet = null;
notiBuilder.setContentIntent(pIntent);
pIntent = null;
NotificationManager notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notiManager.notify(Integer.parseInt(id), notiBuilder.build());
} else {
notiManager.notify(Integer.parseInt(id), notiBuilder.getNotification());
}
获得我的 Int 的方法:
this.eventID = getIntent().getIntExtra("eventID", -1);
每次单击通知时,我都会转到活动但 getExtra 返回-1。