当应用程序在后台时,Firebase 通知数据不回调 didReceiveRemoteNotification
Firebase notification data not callback didReceiveRemoteNotification when app in background
我正在尝试使用一些自定义数据将有效负载通知从 php 发送到 ios,当应用程序处于后台并收到通知时,然后在该通知上单击选项卡,应用程序已打开但它没有调用 didReceiveRemoteNotification 函数。仅当 JSON 具有 data 且没有 notification 参数时才有效。
JSON 从 PHP 发送到 firebasr API
{
"to":"[Token]",
"priority":"high",
"content_available":true,
"notification":{
"title":"TITLE",
"body":"Body",
"sound":"default",
"badge":"1"
},"data":{
"type":"link",
"image":"",
"link":"",
"itemId":"",
"categoryId":"",
"groupId":""
}
}
ios代码
func application(application: UIApplication, didReceiveRemoteNotification userInfo:[NSObject : AnyObject],
fetchCompletionHandler completionHandler:
(UIBackgroundFetchResult) -> Void){
print("vvvvvvvvvvvvv=%@", userInfo)
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if #available(iOS 8.0, *) {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
} else {
let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
application.registerForRemoteNotificationTypes(types)
}
FIRApp.configure()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification),name:kFIRInstanceIDTokenRefreshNotification, object: nil)
}
func applicationDidBecomeActive(application: UIApplication) {
connectToFcm()
}
func applicationDidEnterBackground(application: UIApplication) {
FIRMessaging.messaging().disconnect()
print("Disconnected from FCM.")
}
经过大量测试后,我发现 "content available" 应该在通知中并且应该像这样 "content-available",问题是 firebase 重新构造了 JSON一种使 ios 可读的方法,因此您需要检查他们在每次测试中如何向您发送数据。
{
"to":"[Token]",
"priority":"high",
"notification":{
"title":"TITLE",
"body":"Body",
"sound":"default",
"badge":"1",
"content-available":true,
},"data":{
"type":"link",
"image":"",
"link":"",
"itemId":"",
"categoryId":"",
"groupId":""
}
}
我正在尝试使用一些自定义数据将有效负载通知从 php 发送到 ios,当应用程序处于后台并收到通知时,然后在该通知上单击选项卡,应用程序已打开但它没有调用 didReceiveRemoteNotification 函数。仅当 JSON 具有 data 且没有 notification 参数时才有效。
JSON 从 PHP 发送到 firebasr API
{
"to":"[Token]",
"priority":"high",
"content_available":true,
"notification":{
"title":"TITLE",
"body":"Body",
"sound":"default",
"badge":"1"
},"data":{
"type":"link",
"image":"",
"link":"",
"itemId":"",
"categoryId":"",
"groupId":""
}
}
ios代码
func application(application: UIApplication, didReceiveRemoteNotification userInfo:[NSObject : AnyObject],
fetchCompletionHandler completionHandler:
(UIBackgroundFetchResult) -> Void){
print("vvvvvvvvvvvvv=%@", userInfo)
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if #available(iOS 8.0, *) {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
} else {
let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
application.registerForRemoteNotificationTypes(types)
}
FIRApp.configure()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification),name:kFIRInstanceIDTokenRefreshNotification, object: nil)
}
func applicationDidBecomeActive(application: UIApplication) {
connectToFcm()
}
func applicationDidEnterBackground(application: UIApplication) {
FIRMessaging.messaging().disconnect()
print("Disconnected from FCM.")
}
经过大量测试后,我发现 "content available" 应该在通知中并且应该像这样 "content-available",问题是 firebase 重新构造了 JSON一种使 ios 可读的方法,因此您需要检查他们在每次测试中如何向您发送数据。
{
"to":"[Token]",
"priority":"high",
"notification":{
"title":"TITLE",
"body":"Body",
"sound":"default",
"badge":"1",
"content-available":true,
},"data":{
"type":"link",
"image":"",
"link":"",
"itemId":"",
"categoryId":"",
"groupId":""
}
}