cancelAllLocalNotifications在iOS10中不起作用 [英] cancelAllLocalNotifications not working in iOS10

查看:1775
本文介绍了cancelAllLocalNotifications在iOS10中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在添加新通知时从NotificationCenter中删除所有以前的本地通知。但它适用于iOS9.0及更低版本,但在iOS 10中它会触发多个本地通知。所以似乎 cancelAllLocalNotifications 没有清除通知。

I want to remove all previous local notification from NotificationCenter when adding new notifications. But it is working in iOS9.0 and lower version but in iOS 10 it fires multiple local notifications. So it seems like cancelAllLocalNotifications not clearing notifications.

在iOS10中成功编译代码。

Code compile successfully in iOS10.

UIApplication.shared.cancelAllLocalNotifications()


推荐答案

对于iOS 10,Swift 3.0

For iOS 10, Swift 3.0

cancelAllLocalNotifications 已弃用iOS 10。

cancelAllLocalNotifications deprecated from iOS 10.


@available(iOS, introduced: 4.0, deprecated: 10.0, message: "Use UserNotifications Framework's -[UNUserNotificationCenter removeAllPendingNotificationRequests]")
open func cancelAllLocalNotifications()


您必须添加此导入语句,

You will have to add this import statement,

import UserNotifications

获取通知中心。并执行如下操作

Get notification center. And perform the operation like below

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.

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

If you want to remove single or multiple specific notification, you can achieve it by below method.

center.removeDeliveredNotifications(withIdentifiers: ["your notification identifier"])

希望它有所帮助.. !!

Hope it helps..!!

这篇关于cancelAllLocalNotifications在iOS10中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆