-->

Firebase messaging not working for “data” messages

2019-05-29 20:25发布

问题:

I have an app running on iOS 9.3.5. I need to be able to send a push notification to my app when it's in the foreground or background. I don't actually need to include any data, I just need to ping the app so it will "phone home". So I don't need/want the user to see any notifications.

Since the company is already using Firebase for their Android app, I've set that up in the iOS app. If I send a message to https://fcm.googleapis.com/fcm/send with the notification key in the payload, it's received on the iPhone. When I try it with the data key instead, I get nothing. In both cases I get a success response from the POST. I've implemented the follow callbacks:

application:didReceiveRemoteNotification:
userNotificationCenter:willPresentNotification:withCompletionHandler:
applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage

None of those are called when sending a message with the data key.

Edit: This is the payload I'm using.

{ "data": { "message": "phonehome", }, "to" : "xxxxx" }

After doing some more testing it looks like I get that message when the app is in the foreground but not the background. When I switch to the foreground then application:didReceiveRemoteNotification: gets called.

Edit 2: Added content_available and that did the trick. Thanks!

{ "to" : "xxxx", "content_available" : true }

回答1:

It's a little bit hard to suggest without a sample payload, However, do try to use the priority parameter as high or content_available to true.