9 C
New York
Monday, December 16, 2024

android – Solely-data messages being collapsed and dropped in iOS


I wish to ship a number of information messages to my flutter app. I used to be working with notifications as they have been despatched in all states (foreground, background, terminated) however discovered that they’re being collapsed. I switched to make use of only-data messages and after some code modification, it really works flawlessly on Android when the app is in any situation, I all the time obtain the info message. Even when the consumer has no web connection and a number of messages are despatched, each might be acquired after reconnecting. When testing this on iOS, I ran the app in launch mode as I did in Android and all the pieces labored in foreground, after I tried to swipe to house and ship app to background and ship a message, nothing is acquired. After I reopen the app, the info message is then acquired. If I ship a number of information messages, solely the final one is acquired after reopening the app.

This behaviour additionally exists when merely locking the iPhone

There are 2 main issues right here:

1- Information messages aren’t being acquired in background/terminated states in launch mode however works nice in profile mode

2- Even after re-opening the app, solely the final information message is being acquired (much like collapsing notifications in FCM)

I did not discover any feedback relating to this behaviour in FCM doumentation. Can somebody look into this to make sure if this can be a regular behaviour or not. If that’s the case, how can I overcome it or a piece round ?

Information Message Sending Operate:

public operate sendDataMessage($deviceTokens, $information = [], $precedence = "excessive")
{
    $messaging = app('firebase.messaging');
    $clean_data = $information;
    if (isset($information["type"]) && $information["type"] == "chat" && $information["action"] == "receiveMessage") {
        unset($clean_data["fcm_message"]);
    }
    $messages = [];
    foreach ($deviceTokens as $token) {
        attempt {
            if (isset($information["type"]) && $information["type"] == "chat" && $information["action"] == "receiveMessage") {
                $chat_message = $information["fcm_message"];
                $clean_data["title"] = Chats::choose("identify")->discover($information["chat_id"])->identify;
                $clean_data["body"] = $chat_message["sender"]["name"] . ": " . ($chat_message["content"]["type"] == "Textual content" ? $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;
        }
    }
    attempt {
        $report = $messaging->sendAll($messages);
    } catch (NotFound $e) {
        return $e;
    } catch (Exception $e) {
        return $e;
    }

    return $report->successes()->rely();
}

Firebase Handler:

static void initializeFCMHandler() async {
    FirebaseMessaging.onBackgroundMessage(handleBackgroundMessage);
    FirebaseMessaging.onMessage.pay attention(handleMessage);
}

@pragma('vm:entry-point')
static Future 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);
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles