以下代码显示通知并发送int数据,但在其他活动getExtras()
中返回null
。为什么呢?
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);
答案 0 :(得分:15)
可能PendingIntent已经存在且未创建,但已重复使用。尝试使用PendingIntent.FLAG_UPDATE_CURRENT
标志创建它:
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
请参阅https://stackoverflow.com/a/9330144/530804以及该问题的其他答案。