Ios 是否取消已传递的UILocalNotification?
有可能这样做吗Ios 是否取消已传递的UILocalNotification?,ios,uilocalnotification,uiapplication,Ios,Uilocalnotification,Uiapplication,有可能这样做吗UIApplication的ScheduledLocalNotification似乎不会返回已经发送到用户通知中心的通知,因此我认为这可能是出于设计,但我找不到任何文件证明 有人知道吗 谢谢 编辑:找到以下内容: 您可以通过调用取消特定的计划通知 cancelLocalNotification:在应用程序对象上,您可以取消 通过调用cancelAllLocalNotifications来调用所有计划通知。 这两种方法还以编程方式消除当前 在这里: 但是,如果scheduledLoc
UIApplication的
ScheduledLocalNotification
似乎不会返回已经发送到用户通知中心的通知,因此我认为这可能是出于设计,但我找不到任何文件证明
有人知道吗
谢谢
编辑:找到以下内容:
您可以通过调用取消特定的计划通知
cancelLocalNotification:在应用程序对象上,您可以取消
通过调用cancelAllLocalNotifications来调用所有计划通知。
这两种方法还以编程方式消除当前
在这里:
但是,如果scheduledLocalNotifications
未向我提供已传递的通知,我如何获取对已传递通知的引用
编辑2:
在我注册了一些通知之后,下面是我要做的:
UIApplication *app = [UIApplication sharedApplication];
for (UILocalNotification *localNotification in app.scheduledLocalNotifications)
{
if (someCondition) {
[app cancelLocalNotification:localNotification];
}
}
}
问题是,一旦交付,它们就不再处于“scheduledLocalNotifications”中。请尝试以下链接:
本地通知正在注册到设备通知中心,而不是在应用程序中
但是,如果您的应用程序正在运行,并且是一个通知时间,则您可以在游戏中获取通知参数,具体如下:
-(void) application:(UIApplication*)app didReceiveLocalNotification:(UILocalNotification*) notification
{
// local notification is geted
}
看到更新后的代码后,您似乎对使用in-for循环取消所有通知感兴趣,这样您就可以使用-
[[UIApplication sharedApplication] cancelAllLocalNotifications];
您可以通过将新创建的通知添加到自己的
NSMutableArray
通知中来解决此问题,并检查该数组而不是app.scheduledLocalNotification
。
大概是这样的:
将NSMutableArray
添加到ViewController.h文件:
NSMutableArray *currentNotifications;
启动ViewController时启动它
currentNotifications = [[NSMutableArray alloc] init];
启动通知时,还要将其添加到阵列中:
UILocalNotification *notification = [[UILocalNotification alloc] init];
...
[currentNotifications addObject:notification];
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
for (UILocalNotification *notification in currentNotifications) {
if (someCondition) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
[currentNotifications removeObject:notification];
}
}
稍后,如果要取消该通知,请在数组中查找它。
还要将其从阵列中删除:
UILocalNotification *notification = [[UILocalNotification alloc] init];
...
[currentNotifications addObject:notification];
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
for (UILocalNotification *notification in currentNotifications) {
if (someCondition) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
[currentNotifications removeObject:notification];
}
}
我也一直在寻找答案。我的问题是,我想“清理”位于通知中心的所有与应用程序相关的通知,以防用户从其dock图标打开应用程序(因为这对以前触发的通知没有任何影响…) 幸运的是,
cancelAllNotifications
似乎不仅取消了预定的通知,还取消了所有通知。因此,我只是保留了对现有计划通知的引用,然后对其进行了相应的重新安排:
UIApplication *app = [UIApplication sharedApplication];
NSLog(@"\nScheduled notif count (prior) = %d", app.scheduledLocalNotifications.count);
NSArray *scheduledNotifs = app.scheduledLocalNotifications; // hold on to a reference
[[UIApplication sharedApplication] cancelAllLocalNotifications]; // blast everything
NSLog(@"\nScheduled notif count (post-wipeout) = %d", app.scheduledLocalNotifications.count);
for (UILocalNotification *notif in scheduledNotifs) {
[app scheduleLocalNotification:notif]; // put them back
}
NSLog(@"\nScheduled notif count (post-repopulation) = %d", app.scheduledLocalNotifications.count);
不确定这是否对任何人都有帮助,但这对我的情况非常有用。如果您还将通知保存在NSUserDefaults中,则可以在安排通知时使用NSKeyedArchiver将其转换为NSData 要将其作为UILocalNotification返回,请使用NSKeyedUnarchiver。然后可以使用cancelLocalNotification方法删除它
完全解释(Swift版本+链接到原始Obj-C解决方案)自iOS10以来,如果您已经转换到使用
UNUserNotificationCenter
,现在就有了对该解决方案的本机支持
苹果文档声明:
func getDeliveredNotifications(completionHandler:@escaping([UNNotification])->Void)
为您提供仍显示在Notification Center中的应用程序通知列表
func removeDeliveredNotifications(带标识符:[String])
从通知中心删除指定的通知
func removeAllDeliveredNotifications()
从通知中心删除应用程序的所有通知
你们能展示一些代码,你们是如何使用的吗?当然,只是用一些代码更新了问题。是的,我已经读过了。但是我想做的是在以后(任意)的时间删除一个特定的通知,而不仅仅是在第一次收到通知的时候。在我的实际代码中,我有一个关于[app cancelLocalNotification:localNotification]的条件,所以CancelAllLocalNotification不会起作用。我将更新我的代码段以与此匹配。只要你的应用保持驻留状态,此操作就可以正常工作。一旦辞职,
currentNotifications
将被删除。我希望有一种方法也能列出已发送的通知,@elsurudo。这对我来说不适用于ios 9.3.5,通知会留在通知中心