We are using FCM to send remote notifications for both iOS and Android. The following are the payloads we are sending from the backend.
options = {
notification: {
title: "title",
body: body,
sound: 'default'
},
priority: "high",
content_available: true,
data: {
type: 'type',
id: id,
}
}
This works for ios and android.
But for some reason, the android side we need to send title
,body
and sound
for keys in data
payload and need to remove notification
payload.
Now the notifications are not receiving ios side when app is not active, banner notifications are not arriving but the data is receiving when app is active. We need banners on the iOS side.
Is that notification
key is mandatory to display banner
in iOS?
How to use the same payload for both iOS and Android.
options = {
priority: "high",
content_available: true,
data: {
title: "title",
body: body,
sound: 'default'
type: 'type',
id: id,
}
}
Also tried adding content_available
and priority
keys with various combinations.
Gone through all FCM docs and it still confuses. Help/Suggestions appreciated.
Once your client app is installed on a device, it can receive messages through the FCM APNs interface. You can immediately start sending notifications to user segments with the Notifications composer, or messages built on your application server.
Firebase Cloud Messaging (FCM) is a messaging solution that lets you reliably send messages at no cost to both Android & iOS devices. Using FCM, you can send data payloads (via a message) to a device for a specific application. Each message can transfer a payload of up to 4KB to a client.
For Apple client apps, you can receive notification and data payloads up to 4000 bytes over the Firebase Cloud Messaging APNs interface.
A recent feature was added for FCM that gives an option to provide specific params for specific platforms, called Platform Overrides:
Customizing a message across platforms
Messages sent by the FCM v1 HTTP protocol can contain two types of JSON key pairs:
- a common set of keys to be interpreted by all app instances that receive the message.
- platform-specific blocks of keys interpreted only by app instances running on the specified platform.
- Platform-specific blocks give you flexibility to customize messages for different platforms to ensure that they are handled correctly when received. In many scenarios, it makes sense to use both common keys and platform-specific keys in a given message.
When to use common keys
- Whenever you're targeting app instances on all platforms — iOS, Android, and web
- When you are sending messages to topics
The common keys that are interpreted by all app instances regardless of platform are message.notification.title, message.notification.body, and message.data.
When to use platform-specific keys
- When you want to send fields only to particular platforms
- To send platform-specific fields in addition to the common keys
Whenever you want to send values to specific platforms only, don't use common keys; use platform-specific key blocks. For example, to send a notification to only iOS and web but not Android, you must use two separate blocks of keys, one for iOS and one for web.
When you are sending messages with specific delivery options, use platform-specific keys to set them. You can specify different values per platform if you want; but even when you want to set essentially the same value across platforms, you must use platform-specific keys. This is because each platform may interpret the value slightly differently — for example, time-to-live is set on Android as an expiration time in seconds, while on iOS it is set as an expiration date.
Example: notification message with platform-specific delivery options
The following v1 send request sends a common notification title and content to all platforms, but also sends some platform-specific overrides. Specifically, the request:
- sets a long time-to-live for Android and Web platforms, while setting the APNs (iOS) message priority to a low setting
- sets the appropriate keys to define the result of a user tap on the notification on Android and iOS — click_action, and category, respectively.
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Match update",
"body":"Arsenal goal in added time, score is now 3-0"
},
"android":{
"ttl":"86400s",
"notification"{
"click_action":"OPEN_ACTIVITY_1"
}
},
"apns": {
"headers": {
"apns-priority": "5",
},
"payload": {
"aps": {
"category": "NEW_MESSAGE_CATEGORY"
}
}
},
"webpush":{
"headers":{
"TTL":"86400"
}
}
}
}
See the HTTP v1 reference documentation for complete detail on the keys available in platform-specific blocks in the message body. For more information about building send requests that contain the message body, see Build Send Requests.
Below payload can be used as a common payload for android and iOS. "score" is just a dummy keyword for explanation and you can add more but all the values should be in String.
{
priority: "high",
tokens: [tokens],
data: {
title: <title>,
body:<body>, <-- this data will be used by the android
score: 345.
},
apns: {
headers: {
"apns-priority": "10"
},
payload: {
aps: { <-- payload for iOS
alert: {
title: <title>,
body:<body>
},
data:{
score: 345
}
}
}
}
}
In the above payload,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With