【问题标题】:ios 10 Firebase Push Notification delegate method not called in background modeios 10 Firebase推送通知委托方法未在后台模式下调用
【发布时间】:2016-10-27 23:56:26
【问题描述】:

我已经为这个问题苦苦挣扎了好几天,我查看了每一篇关于这个问题的帖子,但没有任何效果。

我正在使用 FCM(Firebase 云消息传递)。 FCM 在前台直接与应用对话,但当应用在后台时它会使用 APNS。

每当应用程序通过调用推送通知的委托函数中的方法获得通知时,我都会尝试调用一个方法

我有这 2 个委托函数: func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 根据苹果文档,它不会在后台被调用。

我也有这个函数,我认为应该在后台调用它,但它没有。 func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

我在 ViewController 中拥有所有这些功能

在后台模式下,我收到横幅通知,但没有调用任何代理。有没有办法让它们在后台工作,如果不是我该怎么办? 此外,每当我在应用程序处于后台时发送有效负载时,我也会在日志中收到此错误:<FIRMessaging/WARNING> FIRMessaging receiving notification in invalid state 2

这是我使用 postmate 发送的有效负载:

{ "priority":"high", "notification":{ "sound": "default", "badge": "1", "title":"mytitle", "body":"mybody", "message":"Hello" }, "content_available":true, "to" : "/topics/myTopic" } 我已经在功能中打开了推送通知,并在功能下检查了后台模式下的推送通知。

我使用 Xcode 8 并且一直在 iphone 6 iOS 10.0.2 上进行测试

【问题讨论】:

    标签: ios swift firebase push-notification firebase-cloud-messaging


    【解决方案1】:

    一个协议只能有 1 个委托。

    在这种情况下,我使 APPDelegate 和 ViewController 符合 UIApplicationDelegate。并且只调用了 APPDelegate 中的委托函数。这就是为什么没有调用 ViewController 中的那个。

    解决方案只需将didReceiveRemoteNotification 放在 AppDelegate 中即可。

    【讨论】:

      猜你喜欢
      • 2017-04-07
      • 2021-05-02
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      相关资源
      最近更新 更多