【问题标题】:Is there a callback to the app delegate when a remote notification is received?收到远程通知时是否会回调应用程序委托?
【发布时间】:2016-08-29 02:01:39
【问题描述】:

在收到远程通知并且用户采取行动(关闭/关闭除外)后,应用委托会收到回调:

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(无效 (^)(UIBackgroundFetchResult))completionHandler

或者如果应用注册了通知操作:

-(void) application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler

我的问题是,当收到远程通知时,应用程序是否会收到回调?那是在用户采取任何行动之前。感谢您的意见。

【问题讨论】:

  • @SausageMachine 澄清了我的问题...
  • 为什么在发送到应用之前需要知道通知已经到达设备?
  • @rmaddy 我们正在设计一个系统,后端人员问我是否可以在收到通知时回电委托...我只是想仔细检查一下 SO社区。感谢您的回复
  • @SausageMachine 我请求区别。我喜欢与其他开发人员交谈。感谢您的参与。
  • @Loozie。可以向应用程序发送应用程序定向推送(称为静默或后台推送)。如果用户需要查看某些内容,该应用程序可以接收到该信息,然后发布本地通知。因此实际上这与您所要求的相同(因为用户可以看到通知并且应用程序知道通知在那里,因为它把它放在这里)。但是,如果应用已被用户强行终止,则后台推送将不会传递到应用。

标签: ios push-notification


【解决方案1】:

不,应用没有收到通知已到达设备的任何指示。仅当通知实际发送到应用程序时,它才会收到委托调用,正如您在问题中的两种情况中所描述的那样。

【讨论】:

    【解决方案2】:

    是的,当收到远程通知时,应用程序当然会回调这些委托方法。

     func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        print("DEVICE TOKEN = \(deviceToken)")
    }
    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        print(error)
    }
    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        print(userInfo)
    }
    

    【讨论】:

    • didReceiveRemoteNotification 方法在应用程序处于前台时调用,但在应用程序未运行或未由用户终止的情况下调用。
    • 是的,当然这就像当您的应用程序终止时您无法收到任何回调,但是当您点击该通知以打开应用程序或您在点击时执行的任何操作时您可以收到回调.下面是检查通知响应的委托方法。检查我的另一个答案。 @Sumeet.Jain
    【解决方案3】:
    extension AppDelegate: UNUserNotificationCenterDelegate {
    
        public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            completionHandler([.alert,.sound,.badge])
            print("NOtification received")
        }
    
        public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    
        print("original body was : \(response.notification.request.content.title)")
        print("Tapped in notification")
    
        }
    }
    

    【讨论】:

    • 正是我想要的谢谢。我收到通知的回调:func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {} 通知的回调打开:func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {}
    【解决方案4】:

    可以使用后台更新通知,如https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app 中所述 但是,此解决方案的用例受限于允许以这种方式发送的通知数量较少(文档当前声明“不要尝试每小时发送超过 2 或 3 个”)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      相关资源
      最近更新 更多