我使用下面的代码创建一个通知:如何在不影响未决意图及其有效载荷的情况下更新通知?
Intent intent = new Intent(this, GetStockQuote.class);
intent.putExtra("abc", abcObject);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
/* Build the notification */
Notification notification = new Notification.Builder(this)
.setContentTitle(abcObject.getCode())
.setContentText(abcObject.getText())
.setAutoCancel(false)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notificationID,notification);
Log.i(TAG,"notification.notified");
正如你可以看到连接到该通知的PendingIntent
携带的有效载荷。这是一个自定义类的对象。
现在我更新服务中的通知。我明白,如果您必须更新通知(不创建新通知),则必须指定我正在执行的相同notificationID
。
这是用于更新上面创建的通知服务的代码:
Intent intent = new Intent(this, GetStockQuote.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification=new Notification.Builder(this)
.setContentTitle(newTitle)
.setContentText(newBody)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.build();
/*Get instance of Notification Manager and show the notification*/
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notificationID,notification);
代码更新与新内容的现有通知,但PendingIntent
不包含有效载荷。
我无法访问服务中的有效负载。因此,我想只更新通知服务,并使用新的textContent
,而不影响在创建过程中设置的有效负载。
现在的问题是我有很多这样的通知。它们中的每一个都有唯一的有效载荷,但Intent
中的目的地类别保持不变。
更新通知时是否有办法保留有效负载?
当您启动一个服务,您可以考绩你的对象的服务,让您可以在服务访问它。 – 2014-09-27 17:24:55
@MisaghEmamverdi nope,那是不可能的。该服务启动一次,并更新所有通知处于活动状态。 – Ashwin 2014-09-27 17:27:56
你的意思是服务应该将不同的额外对象传递给待处理的意图? – 2014-09-27 17:36:27