Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FCM Firebase push notification Android/iOS

I'm using FCM to send notifications for a project in booth plattforms, iOS and Android. Following are the payloads I'm sending:

{ 
  "to":"user_key",
  "priority":"high",
  "content_available":true,
  "mutable_content":true,
  "data":{
        "body":"test"
  }
}

After reading the Firebase documentation, the behaviour when sending notification and data payload through FCM is:

iOS

  • You won't see banner if notification -> body is not defined.

Android

  • If you pass data payload only, it will receive data when is in background.
  • If you pass notification_payload, system tray will handle it.

The thing is:

For Android I want to avoid to pass notification_payload in order to receive data when app is in background (in this way onMessageReceived() will be called). But then, I won´t receive notifications in iOS.

Help/Suggestions appreciated.

like image 263
IrApp Avatar asked Nov 08 '22 00:11

IrApp


1 Answers

Server should use different json structures for IOS and Android push request. For example:

Android:

[data] => Array
        (
            [title] => Test
            [message] => Message
        )

IOS:

[notification] => Array
        (
            [title] => Title
            [body] => Body
            [sound] => 1
            [vibrate] => 1
        )

[data] => Array
        (
            [custom_key] => custom_value
        )

You can edit it according to your task.

like image 148
Onix Avatar answered Nov 14 '22 23:11

Onix