【发布时间】:2017-06-30 07:42:36
【问题描述】:
我遇到了一个涉及停止本地通知的问题。这个停止按钮是这样的,所以如果用户启动一个计时器(一旦计时器用完就会弹出一个通知),然后决定他们不再需要这个计时器,他们可以取消它。这里的问题是我尝试过的每种方法都不会阻止通知关闭。这主要是因为这些方法已经过时了。下面是我的代码,我想知道是否有人对如何阻止通知关闭有任何想法。任何帮助将不胜感激。
//In the view did load section
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
print("granted")
} else {
print("not granted")
}
}
//in a seperate function
let center = UNUserNotificationCenter.current()
content.title = "Local notification"
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.init(named: "1.mp3")
content.badge = 1
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: time, repeats: false)
let request = UNNotificationRequest(identifier: "requestAlarm", content: content, trigger: trigger)
center.add(request)
center.delegate = self
【问题讨论】:
-
我没有看到任何取消代码。
-
是的,我已将其删除,因为正如我所提到的,它在 IOS 10 中普遍贬值
标签: swift xcode timer localnotification cancellation