Custom data push message with Firebase with platform-specific notification issue - android

I have some issues with sending custom push message using Firebase Cloud Messaging with my data.
On iOS I have created Notification Service Provider that controlls every incoming notification but I had to add "notification" field with title and body in push request to Firebase. If there was no "notification" field then if app was turned off/killed then I couldn't receive message.
On Android I have my FirebaseMessagingService where I receive messages (and I can decide what to do with it) but if "notification" field is present in Firebase request then FirebaseMessagingService is not receiving message but notification shows automatically with title and body from "notification".
I've found some docs from Firebase about platform-specific messages
(When to use platform-specific keys) and I though to add notification only for iOS platform. I have tried few approches described in link above and here Creating the Remote Notification Payload / Payload Key Reference
Below some examples of JSON I've tried to send but I always get error InvalidParameters: Invalid notification payload (I've tried all possibilities with fields apns, payload, aps, alert)
{
"to": "xyz................", //single device with android or ios
"mutable_content": true,
"data": { "field1": "value1" [.......] },
"apns": {
"payload": {
"title": "Some message title",
"body": "Some message body",
"sound": "default"
}
}
or
{
"to": "xyz................", //single device with android or ios
"mutable_content": true,
"data": { "field1": "value1" [.......] },
"apns": {
"payload": {
"aps": {
"title": "Some message title",
"body": "Some message body",
"sound": "default"
}
}
}
etc.
The purpose is to receive data in my app and decide how to handle it - depending on incoming data I want to show my own notification or make some custom action in app.
Is it even possible to send platform-specific message to iOS with notification/alert and without it to Android? If so - how?

Related

android read FCM data from notificationManager.getActiveNotifications

I'm using Capacitor for my App, I have an use case similar to whatsapp, when users receive a new message if your app is in background, you receive FCM notification in the notification tray, later if user open that particular chat directly by opening the app(without clicking on the notification), i'd like to clear that notification from the notification tray.
so here's what I thought
get active notification
find the active chat notifications by filtering extra data sent by FCM
clear those notifications programatically
however android's notificationManager.getActiveNotifications doesn't seem to provide access to FCM data.
below is the payload i'm posting to FCM(i want to use chatId and type in data section to clear active notifications).
{
"android": {
"priority": "high"
},
"notification": {
"title": "you have a new message from XYZ",
"body": "hello"
},
"data": {
"messageId": "62971793-0111-4c9b-bbb2-bb78262a0543",
"chatId": "631e162f5f286c23d13e06d5",
"type": "CHAT_NEW_MESSAGE"
},
"token": "fcmToken"
}
Here's the capacitor plugins Android Logic to get the delivered/active notification.
how can I achieve above use case?

Flutter / Firebase push notifications not fired when app is closed

I am using firebase messaging to send notifications to users in flutter application, and everything is working fine when I am sending notification from postman (notification always shows), but then when I want to send the same notification from my backend server, notification not showing on device. On server logs I can see that notification was send with success like this:
{
"multicast_id": XXXXXXXXXXXXX,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "XXXXXXXXXXXXX"
}
]
}
So it means it was correctly send to messaging services, but push notification not firing on device.
Sometimes when I open app (going to foreground of application) then notification somehow shows, but I need users to see notification always, especially when their app is closed.
I am sending notification with high priority so this is not an issue.
Any help, what could be potential issue with this?
My request to FCM:
POST: https://fcm.googleapis.com/fcm/send
Authorization: key=xxxx_MY_KEY_XXXXX
Content-Type: application/json
{
"to": "xxxxxMY_FCM_TOKEN_xxxxxx",
"mutable_content": true,
"content_available": true,
"priority": "high",
"data": {
"content": {
"id": 106,
"channelKey": "remote_chann",
"title": "Hello",
"body": "test",
"notificationLayout": "Messaging"
}
}
}
As per your data send with fcm is sending only data object not notification object as per the fcm to wakeup it self you need to send notification object along with data object.
https://firebase.google.com/docs/cloud-messaging/concept-options

FCM how to display the push notification preview when receiving a message

I implemented the push notification on Android through Firebase, I have a problem that I haven't found an answer anywhere, I'm getting my push perfectly, but when I receive a notification with the application closed, the default sound is played, but the message does not slide on screen, if I go to the notifications my message is there, but it does not slide as soon as it arrives on the device is this a parameter of the firebase API or is it an implementation in the code? In the app's permissions, I'm allowed to slide messages. Has anyone come across this?
This is the body of the request for the legacy firebase api.
POST - https://fcm.googleapis.com/fcm/send
{
"notification": {
"body": "my body",
"title":"my title",
"sound": "default"
},
"data":{
"click_action": "",
"type": "",
"action":"",
"content":""
},
"priority": "high",
"to": "token device"
}

Send data to Android device Notification to IOS devices

We are using firebase to send notifications to both Android and IOS devices from server and we are using legacy FCM to send notifications.
But when our app is backgrounded notification is handled by system itself and thus we couldn't handle it by app. So we need to add data to notification to handle the notification by application itself on android side.
But due to we also have ios devices we couldn't remove notification from the pattern.
Is there any way to send data to Android devices and notification to IOS devices?
Also you may find the pattern that I mentioned at below.
{
{
"registration_ids":[
],
"content_available":true,
"priority":"high",
"data":{
"title":"",
"text":"",
"badge": 1,
"sound": "default"
},
"notification":{
"title":"",
"body":"",
"badge": 1,
"sound": "default"
}
}
You will have to map the FCM Registration ID with the OS Type and store it somehow. Now when you are sending the notifications, based on the OS Type, you will change the body structure for the notifications.

Data payload not being handled on Android when using `content_available`

I'm trying to send a payload using GCM that can be handled on the background or foreground by clients using Android and iOS. The messages are delivered to clients using both platforms that are subscribed in a topic.
Because I need the iOS app to handle the data even when the app is on the background or closed, I'm setting "content_available": true, which works as expected on iOS.
However, when I add that parameter, Android does not handle the received data and instead displays a blank notification.
Here's an example payload:
{
"to": "/topics/946",
"content_available": true,
"data": {
"time": "2016-02-04 18:33:08",
"message": "Hello",
"msgType": "chat",
"senderId": "50",
"senderName": "John"
}
}
The documentation says that data messages should wake the Android app anyway:
On iOS, use this field to represent content-available in the APNS
payload. When a notification or message is sent and this is set to
true, an inactive client app is awoken. On Android, data messages wake
the app by default. On Chrome, currently not supported.
What am I doing wrong?
EDIT: I've already tried using "priority":"high", which does not make any difference.

Categories

Resources