简体   繁体   English

可以迅速了解通知是在3发出的吗?

[英]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.

相关问题 Swift UILocalNotification:显示通知时是否可以触发事件? - Swift UILocalNotification: Is it possible to fire an event when Notification is displayed? Swift - 如何在键盘抬起时滚动 collectionView 的内容 - Swift -How to scroll the collectionView's contents when the keyboard is raised AVCaptureDeviceSubjectAreaDidChange 当此通知发布在 swift - AVCaptureDeviceSubjectAreaDidChange when this notification is post in swift 应用程序在后台 swift 3 时的远程推送通知 - remote push Notification when app is in background swift 3 在 Swift 5 中点击通知时切换 ViewControllers - Switching ViewControllers when tapping a notification in Swift 5 应用程序终止时的 Swift IOS 启动通知 - Swift IOS launch notification when app terminated 当应用程序在后台运行时,swift 4通知不出现 - swift 4 notification not appearing when app is in background 删除UITableView行时删除通知(快速) - Remove Notification when UITableView row is deleted (Swift) swift4:从服务器发送通知时,Firebase通知不起作用 - swift4: Firebase Notification not working when sending notification from server 收到通知时点击打开本地通知 - iOS - Swift - Open Local Notification on tap when Notification received - iOS - Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM