简体   繁体   English

在后台模式下未调用ios 10 Firebase推送通知委托方法

[英]ios 10 Firebase Push Notification delegate method not called in background mode

I have been struggling with this problem for days and I have looked at every post about this and nothing works. 几天来我一直在努力解决这个问题,我看过每一篇关于这个问题的文章并没有任何效果。

I am using FCM (Firebase Cloud Messaging). 我正在使用FCM(Firebase云消息传递)。 FCM talks directly to the app when in foreground, but it will use APNS when the app is in background. FCM在前台时直接与应用程序对话,但在应用程序处于后台时它将使用APNS。

I am trying to call a method everytime the app got notification by calling the method in the push notification's delegate function 我试图通过调用推送通知的委托函数中的方法,每当应用程序收到通知时调用方法

I have these 2 delegate functions: func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) and func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) which does not get called in background, according to apple documentation. 我有这两个委托函数: func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)根据苹果文档,它不会在后台调用。

I also have this function which I believe should get called in the background but it does not. 我也有这个功能,我认为应该在后台调用,但事实并非如此。 func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

I have all of these functions in ViewController 我在ViewController中拥有所有这些功能

In background mode, I get banner notifications, but none of the delegate is called. 在后台模式下,我收到横幅通知,但没有调用任何委托。 Is there any way to make any of them work in the background, if not what should I do? 有没有办法让它们中的任何一个在后台工作,如果不是我该怎么办? In addition I also get this error : <FIRMessaging/WARNING> FIRMessaging receiving notification in invalid state 2 in the log everytime I send a payload while the app is in the background. 此外,我还收到此错误: <FIRMessaging/WARNING> FIRMessaging receiving notification in invalid state 2每当我在应用程序处于后台时发送有效负载时, <FIRMessaging/WARNING> FIRMessaging receiving notification in invalid state 2在日志中<FIRMessaging/WARNING> FIRMessaging receiving notification in invalid state 2

Here is the payload that I send using postmate: 这是我使用postmate发送的有效负载:

{ "priority":"high", "notification":{ "sound": "default", "badge": "1", "title":"mytitle", "body":"mybody", "message":"Hello" }, "content_available":true, "to" : "/topics/myTopic" } I already turned on push notification in capabilities and checked push notification in background modes under capabilities. { "priority":"high", "notification":{ "sound": "default", "badge": "1", "title":"mytitle", "body":"mybody", "message":"Hello" }, "content_available":true, "to" : "/topics/myTopic" }我已经在功能中启用了推送通知,并在功能下以后台模式检查了推送通知。

I use Xcode 8 and have been testing on iphone 6 iOS 10.0.2 我使用Xcode 8并且已经在iphone 6 iOS 10.0.2上进行了测试

There can only be 1 delegate for a protocol. 协议只能有1个代表。

In this case, I made APPDelegate and ViewController conform to UIApplicationDelegate. 在这种情况下,我使APPDelegate和ViewController符合UIApplicationDelegate。 And only delegate functions in APPDelegate got called. 并且只调用APPDelegate中的委托函数。 That's why the one in ViewController is not called. 这就是为什么没有调用ViewController中的那个。

The solution would be just put didReceiveRemoteNotification in the AppDelegate. 该解决方案将只是把didReceiveRemoteNotification在AppDelegate中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM