I want to send multiple data messages to my flutter app. I was working with notifications as they were sent in all states (foreground
, background
, terminated
) but found that they are being collapsed. I switched to use only-data messages and after some code modification, it works flawlessly on Android
when the app is in any condition, I always receive the data message. Even if the user has no internet connection and multiple messages are sent, each one will be received after reconnecting. When testing this on iOS
, I ran the app in release mode as I did in Android
and everything worked in foreground, when I tried to swipe to home and send app to background and send a message, nothing is received. When I reopen the app, the data message is then received. If I send multiple data messages, only the last one is received after reopening the app.
This behaviour also exists when simply locking the iPhone
There are 2 major problems here:
1- Data messages are not being received in background/terminated states in release mode but works fine in profile mode
2- Even after re-opening the app, only the last data message is being received (similar to collapsing notifications in FCM)
I didn’t find any comments regarding this behaviour in FCM doumentation. Can someone look into this to ensure if this is a normal behaviour or not. If so, how can I overcome it or a work around ?
Data Message Sending Function:
public function sendDataMessage($deviceTokens, $data = [], $priority = "high")
{
$messaging = app('firebase.messaging');
$clean_data = $data;
if (isset($data["type"]) && $data["type"] == "chat" && $data["action"] == "receiveMessage") {
unset($clean_data["fcm_message"]);
}
$messages = [];
foreach ($deviceTokens as $token) {
try {
if (isset($data["type"]) && $data["type"] == "chat" && $data["action"] == "receiveMessage") {
$chat_message = $data["fcm_message"];
$clean_data["title"] = Chats::select("name")->find($data["chat_id"])->name;
$clean_data["body"] = $chat_message["sender"]["name"] . ": " . ($chat_message["content"]["type"] == "Text" ? $chat_message["content"]["body"] : $chat_message["content"]["type"]);
}
$messages[] = CloudMessage::withTarget('token', $token)
->withData($clean_data)
->withAndroidConfig(AndroidConfig::fromArray([
'priority' => 'high', // Use high priority
'ttl' => 86400 * 28, // 4 weeks in seconds
]))
->withApnsConfig(ApnsConfig::fromArray([
'headers' => [
'apns-expiration' => strval(time() + 86400 * 28), // Expiry timestamp
'apns-priority' => '10',
],
'payload' => [
'aps' => [
// 'sound' => 'default',
"content-available" => 1,
"mutable-content" => 1
],
],
]));
} catch (Exception $e) {
return $e;
}
}
try {
$report = $messaging->sendAll($messages);
} catch (NotFound $e) {
return $e;
} catch (Exception $e) {
return $e;
}
return $report->successes()->count();
}
Firebase Handler:
static void initializeFCMHandler() async {
FirebaseMessaging.onBackgroundMessage(handleBackgroundMessage);
FirebaseMessaging.onMessage.listen(handleMessage);
}
@pragma('vm:entry-point')
static Future<void> handleBackgroundMessage(RemoteMessage? message) async {
if (message?.notification != null) {
handleNotification(message, isBackground: true);
} else {
handleDataMessage(message, isBackground: true);
}
}
static void handleMessage(RemoteMessage? message) async {
if (message?.notification != null) {
handleNotification(message);
} else {
handleDataMessage(message);
}
}
1