ios - 关闭已经传送的 UILocalNotification?

标签 ios uilocalnotification uiapplication

这有可能吗? UIApplication's scheduledLocalNotifications 似乎没有返回已经发送到用户通知中心的通知,所以我认为这可能是设计使然,但我找不到任何有据可查的证据。

有人知道吗?

谢谢!

编辑:发现这个:

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

此处: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”中。

最佳答案

您可以通过将新创建的通知添加到您自己的 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];
    }
}

关于ios - 关闭已经传送的 UILocalNotification?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10652274/

相关文章:

ios facebook 获取玩相同应用程序的好友列表

ios - CIContext 错误访问崩溃

ios - 在 SFSafariViewController 上设置完成按钮颜色

ios - 使用 UISceneDelegate 将旧 Xcode 项目迁移到新项目

objective-c - 当设置 setStatusBarHidden : in iOS v5 app. 时留有空白。这是修复它的正确方法吗?

ios - CLLocation 是如何实现 Equatable 协议(protocol)的呢?

iphone - 使用本地通知清除应用角标(Badge)

ios - 如何在 iOS 中触发 uilocalnotification 时(而不是在单击时)?

iphone - 本地通知问题

swift - 从 Swift 3 中的自定义键盘打开包含应用程序?