fionbio
fionbio

Reputation: 3554

iOS FCM data only message does not call messaging:didReceiveMessage

Firebase Messaging version 5.6.0. I am attempting to handle a data only message in the foreground via Firebase Messaging on iOS 9.0 (10 if needed), but it is not calling FIRMessagingDelegate's messaging:didReceiveMessage per the documentation. I see the message come in @ FIRMessaging.m's appDidReceiveMessage:message, but never comes through to the delegate.

This is the snippet from the cloud function that sends data to the topic per sending to a topic:

const message = {
  data: {
    test: '123'
  }
  topic: 'example'
}
admin.messaging().send(message);

Did I miss something?

Update: I do receive the data if I implement application:didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler.

Upvotes: 0

Views: 1564

Answers (1)

fionbio
fionbio

Reputation: 3554

Thanks to Kat at Firebase support, here is the answer.

Use legacy sendToTopic instead of send, as send quietly adds content_available=1 which gets treated as APNs silent notification. Here is the updated version:

admin.messaging().sendToTopic('example', {
  data: {
    test: '123'
  }
});
// Always use strings for key/values in the data object.

Below verbatim from Kat at Firebase support:

How the FCM data message is handled would depend on your setting for content_available.

  • If you have content_available = 1, the message is sent via APNs and is treated similar as an APNs silent notification. This is handled in the application:didReceiveRemoteNotification: callback when the app is running in foreground or background (i.e. not killed). See this related StackOverflow post for more information.

  • Without content_available, the message is sent via FCM direct channel. This only handled in the messaging:didReceiveMessage: when app is in foreground.

Note that messages sent via the Admin SDK's send() method uses the FCM HTTP v1 API which have content_available=1 by default, so they are always sent via APNs. If you want to configure the content_available field, you'll need to use the Admin SDK's sendToDevice() method which uses the legacy protocols.

In addition, here is the list of legacy protocols.

Upvotes: 1

Related Questions