解散已经发送的UILocalNotification? [英] Dismiss an already delivered UILocalNotification?
问题描述
有可能这样做吗? UIApplication的
scheduledLocalNotifications
似乎没有返回已经发送到用户通知中心的通知,所以我想这个可能是设计上的,但我找不到任何记录在案的证据。
Is it possible to do this? UIApplication's
scheduledLocalNotifications
doesn't seem to return notifications that have already been delivered to the user's notification center, so I think this may be by design, but I can't find any documented evidence of this.
任何人都知道吗?
谢谢!
编辑:发现:
您可以取消特定的预定时间通过在应用程序对象上调用
cancelLocalNotification:来通知,您可以通过调用cancelAllLocalNotifications取消
所有预定通知。
这两种方法也以编程方式解除当前
You can cancel a specific scheduled notification by calling cancelLocalNotification: on the application object, and you can cancel all scheduled notifications by calling cancelAllLocalNotifications. Both of these methods also programmatically dismiss a currently
但是,如果 scheduledLocalNotifications
,如何获取已发送通知的引用不会给我已经发送的通知吗?
However, how do I get a reference to an already-delivered notification, if scheduledLocalNotifications
doesn't give me notifications that have already been delivered?
编辑2:
这就是我的意思在我注册了一些通知后尝试这样做:
Here's what I'm trying to do, after I've registered some notifications:
UIApplication *app = [UIApplication sharedApplication];
for (UILocalNotification *localNotification in app.scheduledLocalNotifications)
{
if (someCondition) {
[app cancelLocalNotification:localNotification];
}
}
}
问题是,一旦他们'已发送,他们已不在'scheduledLocalNotifications'。
The problem is that once they're delivered, they're no longer in 'scheduledLocalNotifications'.
推荐答案
您可以通过添加新创建的通知来解决此问题您自己的 NSMutableArray
通知并检查该数组而不是 app.scheduledLocalNotifications
。
这样的事情:
You can solve this by adding your newly created notifications to your own NSMutableArray
of notifications and check that array instead of app.scheduledLocalNotifications
.
Something like this:
在Viewcontrollers .h文件中添加 NSMutableArray
:
Add a NSMutableArray
to your Viewcontrollers .h file:
NSMutableArray *currentNotifications;
启动ViewController时启动它
Initiate it when initiating your ViewController
currentNotifications = [[NSMutableArray alloc] init];
发起通知时,还要将其添加到您的阵列中:
When initiating a notification, also add it to your array:
UILocalNotification *notification = [[UILocalNotification alloc] init];
...
[currentNotifications addObject:notification];
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
稍后当您想取消该通知时,请在您的阵列中查找。
同时将其从您的数组中删除:
Later when you want to cancel that notification, look for it in your array instead. Also remove it from your array:
for (UILocalNotification *notification in currentNotifications) {
if (someCondition) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
[currentNotifications removeObject:notification];
}
}
这篇关于解散已经发送的UILocalNotification?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!