Why extra data (integer) is not sent in android notification intent?

点点圈 提交于 2019-12-10 23:50:05

问题


The following code to display a notification and send int data, but in the other activity getExtras() returns null. Why?

int notificationID = 1;
NotificationManager nm = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);


Intent i = new Intent(getApplicationContext(), DownlodResult.class);
i.putExtra("notificationID", 1);


PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);


CharSequence tickerText = "There are updates !";
long when = System.currentTimeMillis();
int icon = R.drawable.ic_launcher;
Notification notification = new Notification(icon,tickerText,when);
CharSequence contentTitle = "There are updates";
CharSequence contentText = "Please click here to view it";
notification.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);



notification.vibrate = new long[] { 100, 250, 100, 500}; // Needs vibrate permissions
nm.notify(notificationID, notification);

回答1:


Probably the PendingIntent exists already and was not created, but reused. Try to create it with the PendingIntent.FLAG_UPDATE_CURRENT flag:

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

See https://stackoverflow.com/a/9330144/530804 and the other answer on that question.



来源:https://stackoverflow.com/questions/10456655/why-extra-data-integer-is-not-sent-in-android-notification-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!