几天前,我一直在努力寻找一种方法来为警报使用自定义意图.虽然我得到了明确的答案,我必须根据一些独特的ID来定制Intents,例如.setAction()还是有一些问题.
我用这种方式定义了PendingIntent:
Intent intent = new Intent(this, viewContactQuick.class);
intent.setAction("newmessage"+objContact.getId());//unique per contact
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK ).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP );
intent.putExtra("id", Long.parseLong(objContact.getId()));
intent.putExtra("results", result.toArray());
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
Run Code Online (Sandbox Code Playgroud)
然后由通知管理器使用
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
// first try to clear any active notification with this contact ID
mNotificationManager.cancel(Integer.parseInt(objContact.getId()));
// then raise a new notification for this contact ID
mNotificationManager.notify(Integer.parseInt(objContact.getId()), notification);
Run Code Online (Sandbox Code Playgroud)
这样工作如下:
问题
对于联系人,这可能会发生多次.并且当生成第二条消息时,通知会很好地提出(消息在那里很好)但是当用户操作它使用旧数据的通知时的意图,所以先前的消息被传递而不是全新的消息.
所以,某种意图是缓存和重用以前的额外内容.如何使每个联系人和每个操作都具有唯一性?