closures已经交付的UILocalNotification?
是否有可能做到这一点? UIApplication's
scheduledLocalNotifications
UIApplication's
通知似乎不会返回已经传递给用户通知中心的通知,所以我认为这可能是devise上的,但是我找不到任何有关这方面的文件证据。
有人知道吗?
谢谢!
编辑:find这个:
您可以通过在应用程序对象上调用cancelLocalNotification来取消特定的预定通知,并且可以通过调用cancelAllLocalNotifications来取消所有预定的通知。 这两种方法都是以编程方式解除当前
这里: http : //developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html
但是,如果scheduledLocalNotifications
未给我已经交付的通知,那么如何获得已交付通知的引用?
编辑2:
我注册了一些通知之后,我正在试图做的是:
UIApplication *app = [UIApplication sharedApplication]; for (UILocalNotification *localNotification in app.scheduledLocalNotifications) { if (someCondition) { [app cancelLocalNotification:localNotification]; } } }
问题是,一旦他们交付,他们不再处于“scheduledLocalNotifications”。
您可以通过将您新创build的通知添加到您自己的NSMutableArray
通知中来解决此问题,并检查该数组而不是app.scheduledLocalNotifications
。 像这样的东西:
添加一个NSMutableArray
到你的ViewControllers .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]; } }
我一直在寻找这个答案。 我的问题是,我想“清理”所有与通知中心相关的应用相关通知,以防万一用户从其停靠栏图标打开应用程序(因为这对于以前触发的通知没有任何作用)
幸运的是,看起来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);
不知道这是否有助于任何人,但这对我的情况很好。
如果你已经转换到使用UNUserNotificationCenter
那么现在iOS10已经有了对它的原生支持。
苹果文档陈述:
func getDeliveredNotifications(completionHandler: @escaping ([UNNotification]) -> Void)
为您提供仍在通知中心中显示的应用通知列表。
func removeDeliveredNotifications(withIdentifiers: [String])
从通知中心删除指定的通知。
func removeAllDeliveredNotifications()
从通知中心移除所有应用的通知。
试试这个链接:
苹果文档
一些教程
而本地通知正在注册到设备通知中心,而不是在您的应用程序。
但是,如果您的应用程序正在运行,并且是通知时间,那么您可以在游戏中获取通知参数:
-(void) application:(UIApplication*)app didReceiveLocalNotification:(UILocalNotification*) notification { // local notification is geted }
你可以这样做,当你安排通知,如果你也保存在NSUserDefaults,使用NSKeyedArchiver将其转换为NSData。
为了使它恢复为UILocalNotification你使用NSKeyedUnarchiver。 然后,您可以使用cancelLocalNotification方法将其删除。
在这里完全解释(Swift版本+链接到原始Obj-C解决scheme)
看到你的更新代码后,似乎你有兴趣取消所有的通知,在for循环,所以你可以使用 –
[[UIApplication sharedApplication] cancelAllLocalNotifications];