设置UILocalNotification的超时时间(过一段时间后从锁屏和通知中心删除它) [英] Set timeout for UILocalNotification (remove it from lockscreen and notification center after some time)

查看:105
本文介绍了设置UILocalNotification的超时时间(过一段时间后从锁屏和通知中心删除它)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个UILocalNotification,它将在五分钟后从锁定屏幕和通知中心消失(如果用户不点击它的话).

I would like to set a UILocalNotification that will disappear from the lock screen and the notification center after five minutes (if the user won't tap on it).

我可以设置通知的超时时间吗?还是触发另一个通知将其删除?

Can I set a timeout for the notification? Or maybe fire another notification that will delete it?

推荐答案

免责声明:仅当您唤醒应用程序后,该代码才有效.在我的情况下,此代码从application(_:handleActionWithIdentifier:for:completionHandler:)调用,这是从本地通知中触发的,而本地通知又是从蓝牙信标监视中调用的.这样进入蓝牙信标的范围就可以唤醒我的应用程序.取决于您的情况,唤醒应用程序的方式取决于您-例如,这可以是您的后端发出的无提示通知. 我也怀疑iOS会允许计时器运行5分钟,在我的情况下,20秒计时器会以很高的概率触发(尽管我知道并不能保证100%的时间,并且有时会保留通知,但是这并不重要)我的情况).

Disclaimer: The code will work only if you wake the app. In my case this code is called from application(_:handleActionWithIdentifier:for:completionHandler:), which is triggered from a local notification, which in its turn is called from a bluetooth beacon monitoring. So that entering the bluetooth beacon's range wakes the app for me. What will be waking the app your case is up to you - this can be a silent notification form your backend, for example. Also I doubt that iOS would let the timer to run for 5 mins, in my case the 20 seconds timer fires with very high probability (though I understand that it is not 100% guaranteed and sometimes notification might remain, however it is not crucial in my case).

在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)

取消功能:

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)
    }
}

或者,您也可以执行UNUserNotificationCenter.current().removeAllDeliveredNotifications()

这篇关于设置UILocalNotification的超时时间(过一段时间后从锁屏和通知中心删除它)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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