yoyo burger yoyo burger - 4 months ago 241
iOS Question

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.

Answer

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.