解雇已经发送的UILocalNotification?

els*_*udo 9 uiapplication ios uilocalnotification

是否有可能做到这一点?UIApplication's scheduledLocalNotifications似乎没有返回已经发送到用户通知中心的通知,所以我认为这可能是设计的,但我找不到任何记录在案的证据.

谁知道?

谢谢!

编辑:发现这个:

您可以通过在应用程序对象上调用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];
        }
     }
}
Run Code Online (Sandbox Code Playgroud)

问题是,一旦他们被交付,他们就不再是'scheduledLocalNotifications'.

小智 9

您可以通过将新创建的通知添加到您自己NSMutableArray的通知中来解决此问题,并检查该数组而不是app.scheduledLocalNotifications.像这样的东西:

添加NSMutableArray到Viewcontrollers .h文件:

NSMutableArray *currentNotifications;
Run Code Online (Sandbox Code Playgroud)

在启动ViewController时启动它

currentNotifications = [[NSMutableArray alloc] init];
Run Code Online (Sandbox Code Playgroud)

启动通知时,还要将其添加到您的阵列:

UILocalNotification *notification = [[UILocalNotification alloc] init];
...
[currentNotifications addObject:notification];
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
Run Code Online (Sandbox Code Playgroud)

稍后,如果要取消该通知,请在数组中查找.同时将其从数组中删除:

for (UILocalNotification *notification in currentNotifications) {
    if (someCondition) {
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
        [currentNotifications removeObject:notification];
    }
}
Run Code Online (Sandbox Code Playgroud)


Jus*_*tyn 5

从 iOS10 开始,如果您已经过渡到使用UNUserNotificationCenter.

Apple 文档指出:

func getDeliveredNotifications(completionHandler: @escaping ([UNNotification]) -> Void)

为您提供仍显示在通知中心的应用程序通知列表。

func removeDeliveredNotifications(withIdentifiers: [String])

从通知中心删除指定的通知。

func removeAllDeliveredNotifications()

从通知中心删除应用程序的所有通知。