Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS data notifications with FCM

I am using FCM (firebase cloud messaging) to send "custom" data notifications to an IOS app. From what I understand, we use notification messages when you want FCM to handle displaying a notification on your app's behalf. And we use data messages when you just want to process the messages only in your app. It's by design.

Problem I am facing is that that Device/InstandID token is unique to installed app and not the user logged in the app. So to solve this, I am sending intended user tag in the data so it becomes a data message. Since the app handles the data notifications, the didReceiveRemoteNotification() callback is only fired when the app is opened and the notification is only shown than and not instantly when it's sent.

My question is that can I send a custom data notification message and make it appear instantly even when the app is closed.

This is the payload I am sending to FCM:

{
    registeration_ids : [<id_1>, <id_2>],
    data : {
        title   : message_title,
        body    : message_body, 
        intended_user : message_user
    }
}

In android FirebaseMessagingService.onMessageReceived() is invoked even if the app is in the background but in ios didReceiveRemoteNotification() is only invoked when the application is launched so no background messages will appear if you send a data message.

like image 486
Shayan C Avatar asked Jul 03 '16 02:07

Shayan C


2 Answers

Poor Documentation from Firebase!

For iOS, Data messages can't be received when app is killed. But this is not mentioned anywhere in their documentation. So I contacted Firebase and this is what they said:

Though there is a content_available parameter, data messages cannot be received when the app is killed. It can only be received on background and in foreground. If you wish to receive data messages when the app is killed, you need to send it along a display messages.

So its better to use APNS for ios rather if you ever thought of using same payload for android and Ios in FCM.

like image 33
emilpmp Avatar answered Sep 20 '22 23:09

emilpmp


From what I understand so far, there is no way of solving this issue properly on the ios side. It works perfectly on the android side because the application is awoken in all states (foreground, background & closed).

You have two kinds of messages you can send:

A notification-message which is displayed and handled by the operating system directly.

A data-message which is handled by the application.

If you add a custom tag, it now becomes a data-message and would have to be handled by the application. You can add a content_available tag in data-message to let the application know about the message but the problem is that the data-message is only delivered to the application in ios if the application is in the foregrounded (opened) or in the background (minimised). The data-message will not be delivered to the application if the user has "force-closed" the application (even with the background notifications enabled).

Solution is to handle the intended user on the server side and solve the multiple-users to one-device-token problem by maintaining the a one to one device-token to user relationship.

like image 131
Shayan C Avatar answered Sep 18 '22 23:09

Shayan C