PendingIntent 存储在哪里?使用的大陷阱

问题描述

在我的项目中,我尝试了 PendingIntent

的一些可能性和局限性

所以我创建了一些通知,通过点击打开一个活动

Intent intent = new Intent(this,NotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(Constants.INTENT_ITEM_NAME,"ItemName");
intent.putExtra(Constants.INTENT_ITEM_ID,itemId);
PendingIntent pendingIntent = PendingIntent.getActivity(this,intent,0);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID)
// properties set
;

notificationmanager.notify(new Random().nextInt(10),notificationBuilder.build());

开始时一切都很好,直到我通过点按通知将其他项目显示NotificationActivity 中。

这里开始出现问题:在 NotificationActivity 中仍然显示前一项。

经过一番搜索可以解决

PendingIntent pendingIntent = PendingIntent.getActivity(this,PendingIntent.FLAG_UPDATE_CURRENT);

但是:如果我同时有很多通知,那么我总是只看到最后一项,PendingIntent 是为其创建的。

getActivity 的第二个参数是 requestCode。所以当我将解决方案更新为

PendingIntent pendingIntent = PendingIntent.getActivity(this,new Random().nextInt(1000),PendingIntent.FLAG_UPDATE_CURRENT);

然后也支持很多项目,如果由于某种原因随机使我的数字相同,NotificationActivity中的内容将相同。

所以对于 99% 的解决方案是好的,是吗?

再次进入第一个方法

PendingIntent pendingIntent = PendingIntent.getActivity(this,0);

如果我为意图设置了新附加 -> 旧附加将被读取。

换句话说:对于我永远的一项活动,通过new Random().nextInt(1000),Android设备系统中的某处将被存储到1000 PendingIntents >

这里是问题

  • 所有这些 PendingIntent 存储在哪里?
  • 如何清除它?

如何清除它,我的意思是一种删除所有当前制作的 PendingIntents方法,其中一些 Random 作为 requestCode,以便以明确的方式与我一次只能使用一个通知

PendingIntent pendingIntent = PendingIntent.getActivity(this,0);

不同的物品

解决方法

PendingIntent 存储在非持久存储中。当设备重新启动时,它们都消失了。

此外,如果您创建一个 PendingIntent 并将其放入 Notification,一旦 Notification 消失(解除、打开等),PendingIntent 就没有了不再使用,将被删除。

通常您不应为 requestCode 使用随机数,因为这不能保证唯一性。如果您想让多个 PendingIntent 并行存在,您需要找到一种方法来使您的 struct LibraryView: View { @ObservedObject var viewModel = LibraryViewModel() var body: some View { List(selection: $viewModel.selectedNodes) { ForEach(viewModel.nodes,id: \.self) { node in if let childrenNodes = node.childrenValues,childrenNodes.count > 0 { Section(header: Text(node.displayName)) { ForEach(childrenNodes,id: \.self) { childNode in LibraryNodeChildView(node: childNode).contextMenu { Button(action:{ store.dispatch(LibraryAction.removeNode(childNode)) store.dispatch(LibraryThunkCreatorImpl().persistLibraryState()) }){ HStack { Text("Delete") Image(systemName: "trash") } } Button(action:{ }){ HStack { Text("Edit Preferences") Image(systemName: "gear") } } } } } } } }.listStyle(SidebarListStyle()) .onAppear() { store.dispatch(LibraryThunkCreatorImpl().loadLibraryState()) }.onDrop(of: ["public.url","public.file-url"],isTargeted: nil) { (items) -> Bool in if let item = items.first { if let identifier = item.registeredTypeIdentifiers.first { print("onDrop with identifier = \(identifier)") if identifier == "public.url" || identifier == "public.file-url" { item.loadItem(forTypeIdentifier: identifier,options: nil) { (urlData,error) in DispatchQueue.main.async { if let urlData = urlData as? Data { let url = NSURL(absoluteURLWithDataRepresentation: urlData,relativeTo: nil) as URL let thunk = LibraryThunkCreatorImpl().createAddNodeThunk(url: url) store.dispatch(thunk) } } } } } return true } else { print("item not here") return false } } } } struct LibraryNodeChildView: View { let node: LibraryNode var body: some View { HStack { if let image = node.image { Image(nsImage: image) .resizable() .scaledToFit() .frame(width: 15,height: 15) } Text(verbatim: node.displayName).onDrag { NSItemProvider(object: node.url as NSURL) }.allowsHitTesting(false) .font(.subheadline) } } } 独一无二。