首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >cancelAllLocalNotifications不在iOS10工作

cancelAllLocalNotifications不在iOS10工作
EN

Stack Overflow用户
提问于 2016-11-03 09:10:41
回答 3查看 9.5K关注 0票数 14

添加新通知时,我希望从NotificationCenter中删除所有以前的本地通知。但是它在iOS 9.0和更低版本中工作,但是在iOS 10中它会触发多个本地通知。因此,cancelAllLocalNotifications似乎没有清除通知。

在iOS10中成功编译代码。

代码语言:javascript
运行
复制
UIApplication.shared.cancelAllLocalNotifications()
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-11-03 09:28:19

对于iOS 10,SWIFT3.0

cancelAllLocalNotifications不推荐iOS 10。

@available(iOS,简介: 4.0,弃用: 10.0,消息:"Use UserNotifications Framework's -UNUserNotificationCenter removeAllPendingNotificationRequests")

您必须添加这个导入语句,

代码语言:javascript
运行
复制
import UserNotifications

去通知中心。然后执行如下操作

代码语言:javascript
运行
复制
let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications() // To remove all delivered notifications
center.removeAllPendingNotificationRequests() // To remove all pending notifications which are not delivered yet but scheduled.

如果要删除单个或多个特定通知,可以通过以下方法实现。

代码语言:javascript
运行
复制
center.removeDeliveredNotifications(withIdentifiers: ["your notification identifier"])

希望能帮上忙!

票数 37
EN

Stack Overflow用户

发布于 2016-11-08 19:44:57

For iOS 10,目标C

代码语言:javascript
运行
复制
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removeAllDeliveredNotifications];
[center removeAllPendingNotificationRequests];

Swift 4

代码语言:javascript
运行
复制
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

如果要列出所有通知:

代码语言:javascript
运行
复制
func listPendingNotifications() {

    let notifCenter = UNUserNotificationCenter.current()
    notifCenter.getPendingNotificationRequests(completionHandler: { requests in
        for request in requests {
            print(request)
        }
    })
}
票数 18
EN

Stack Overflow用户

发布于 2017-03-10 17:32:39

对于iOS 10,可以使用这种方式删除所有本地通知。我刚测试过了,很管用。

代码语言:javascript
运行
复制
    NSArray *arrayOfLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications] ;
    for (UILocalNotification *localNotification in arrayOfLocalNotifications) {
        [[UIApplication sharedApplication] cancelLocalNotification:localNotification] ;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40397552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档