Chi*_*iko 7 objective-c apple-push-notifications ios uilocalnotification
我想设置一个UILocalNotification会在五分钟后从锁定屏幕和通知中心消失(如果用户不点击它).
我可以为通知设置超时吗?或者可能会发出另一个删除它的通知?
免责声明:仅当您唤醒应用程序时,该代码才会起作用。就我而言,这段代码是从调用的application(_:handleActionWithIdentifier:for:completionHandler:),它是从本地通知触发的,而本地通知又是从蓝牙信标监控调用的。这样,进入蓝牙信标的范围就会为我唤醒应用程序。什么会唤醒您的应用程序取决于您 - 例如,这可以是来自后端的静默通知。另外,我怀疑 iOS 会让计时器运行 5 分钟,在我的情况下,20 秒计时器触发的可能性非常高(尽管我知道这不能 100% 保证,有时通知可能会保留,但这并不重要)我的情况)。
20秒后关闭通知:
if #available(iOS 10.0, *) {
... <setting up an iOS 10 notification content and trigger>
notificationCenter.add(UNNotificationRequest(identifier: "myidentifier",
content: content,
trigger: trigger))
Timer.scheduledTimer(timeInterval: 20.0,
target: self,
selector: #selector(self.cancelDeliveredNotification),
userInfo: nil,
repeats: false)
} else {
let notification = UILocalNotification()
... <setting up an iOS8/9 notification>
Timer.scheduledTimer(timeInterval: 20.0,
target: self,
selector: #selector(self.cancelDeliveredNotification),
userInfo: notification as Any?,
repeats: false)
Run Code Online (Sandbox Code Playgroud)
取消功能:
func cancelDeliveredNotification(_ sender: Timer) {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["myidentifier"])
} else {
if let notificationToCancel = sender.userInfo as? UILocalNotification {
UIApplication.shared.cancelLocalNotification(notificationToCancel)
}
}
Run Code Online (Sandbox Code Playgroud)
或者你也可以这样做UNUserNotificationCenter.current().removeAllDeliveredNotifications()
| 归档时间: |
|
| 查看次数: |
1057 次 |
| 最近记录: |