Ash*_*win 3 android android-pendingintent
我正在使用以下代码创建通知:
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");
Run Code Online (Sandbox Code Playgroud)
如您所见,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);
Run Code Online (Sandbox Code Playgroud)
代码使用新内容更新现有通知,但PendingIntent不包含有效内容.
我无法访问服务中的有效负载.所以,我想用新的更新通知服务textContent而不影响在创建期间设置的有效负载.
现在问题是我有很多这样的通知.它们中的每一个都有一个唯一的有效载荷,但它中的目标类Intent保持不变.
有没有办法在更新通知时保留有效负载?
您正在为所有通知设置具有相同ID的待处理意图.
用这个
PendingIntent pIntent = PendingIntent.getActivity(this, notificationID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)
代替
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
998 次 |
| 最近记录: |