[英]Is it possible to understand when a notification raised in swift 3?
I know how to raise local notifications and show actions on it. 我知道如何引发本地通知并显示相关操作。 When a user clicks on actions I can handle it on below method:
当用户单击操作时,我可以使用以下方法进行处理:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse, withCompletionHandler
completionHandler: @escaping () -> Void) {
// Do something here
if response.notification.request.content.userInfo["backUp"] {
doBackUpHere()
// Do other methods
}
}
However, my situation is a little different. 但是,我的情况有些不同。 I need to do some functions when a notification raised before clicking on it or on it's actions(application is foreground or background).
当在单击通知或其动作(应用程序是前台或后台)之前引发通知时,我需要执行一些功能。 Other methods run when the user clicks on notification
用户单击通知时运行其他方法
func notificationRaised (notification: Notification){
// to do if notification is
if notification.userInfo["backUp"] {
doBackUpHere()
}
}
Update 更新
Please see @Sergey answer and our comments to understand better my propose. 请参阅@Sergey答案和我们的评论,以更好地理解我的建议。
If I understand your question correctly, you want to make an action when a notification arrives, without any user input. 如果我正确理解了您的问题,则希望在通知到达时立即采取措施,而无需任何用户输入。 If so, I suggest you take a look at silent push notifications in iOS - they do just that, unfortunately - not showing in the user interface at all.
如果是这样,我建议您看看iOS中的无提示推送通知 -不幸的是,它们只是这样做-根本不显示在用户界面中。
You could also experiment with local notifications on a timer, but remember that you can only run code when a user taps on a notification and/or one of its actions as the app is not guaranteed to be alive by the time a scheduled local notification is shown. 您还可以在计时器上尝试使用本地通知,但是请记住,只有当用户点击通知和/或其操作之一时,您才可以运行代码,因为不能保证在计划的本地通知发布时该应用程序仍然有效所示。
Have you heard about 'Silent push notifications' ? 您是否听说过“静音推送通知” ? I think it is exactly what you are looking for ;) You can get that notification, do your stuff, and then show a local notification if you want.
我认为这正是您要寻找的;)您可以获取该通知,完成您的工作,然后在需要时显示本地通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.