android read FCM data from notificationManager.getActiveNotifications - android

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?

Related

FCM Server side differentiate between iOS and Android

I am sending push notifications from my server (to a firebase token and not to a topic). I do not differentiate between the platforms, the server just sends the JSON via RestTemplate (Spring Boot App).
If I prepare it for Android, it works, but it does not for for iOS. But if I prepare it for iOS, it does not work for Android.
The case is: I want the notification and the background process (It should display the notification in any case foreground/background/killed and it should take key-value pairs from the data payload).
The problem is: If I use notification in my JSON, the notification would be displayed on iOS and on Android but just the iOS can handle the payload (in foreground/background/killed state). If I remove the notification tag in JSON, then everything works fine (Android and iOS receives the payload in all states, but the iOS does not display the notification).
In Android you can build the notification client side (like me), but in iOS its not possible.
So, how can I send a FCM (JSON) with a firebase-token and it is platform-independent? Both can handle it properly, both receive the payload and displays the message and payload should be handled in every state.
This works on android and displays the message (built client side) but does not work for iOS
{
"content_available": true,
"data":{
"action":"bla",
"value":"true",
"id":"111111"
},
"android":{
"priority":"high"
},
"apns":{
"headers":{
"apns-priority":"10"
}
},
"to":"<MY_FCM_TOKEN>"
}
This displays the FCM for android and iOS, but handles the payload just on iOS
{
"notification":{
"sound":"default",
"title":"Test Title",
"body":"Message of FCM"
},
"content_available": true,
"data":{
"action":"bla",
"value":"true",
"id":"111111"
},
"android":{
"priority":"high"
},
"apns":{
"headers":{
"apns-priority":"10"
}
},
"to":"<MY_FCM_TOKEN>"
}
Or is it possible to declare the notification-tag just for iOS?
Or is it possible to create notifications also client side in iOS when a payload receives?
Before sending a notification on the Server side I am checking whether the the platfomr is iOS or Android by using google's method:
https://iid.googleapis.com/iid/info/IID_TOKEN
Which on success returns a JSON with a 'platform' parameter.
You can find more info here

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

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?

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.

How can I open a url when click on push notification, when app is closed, sent by Firebase?

I receive notifications when the app is closed and when it's open, but when it's closed and I click on the push notification that has a link it only opens the app and doesn't open the browser, and if the app is open and I click on the push notification the browser opens.
How can I open the browser when I click on a push notification that has a URL and when the app is closed.
An FCM with notification payload does not go through the onMessageReceived when your app is not running. It's posted by Google Play Services on your behalf directly to notification shade. Once clicked your main activity is launched and intent extras contain the data payload.
You have two options:
Continue using notification payload and handle the data in your main activity onCreate.
That's the activity started by launcher. Use this for information push notifications. If your app is not running it won't be started, which saves battery.
Don't use notification payload.
Use this for business logic push messages. This will always start your app process and let you handle the message in onMessageReceived.
Read more here:
https://firebase.google.com/docs/cloud-messaging/android/receive
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
Use the below payload if you want to open the WEB URL while clicking on the FCM notification when the app is Killed or in the Background.
{
"to": "device-token",
"priority":"high",
"data":{
"body": "there",
"title": "Hi",
"LinkUrl": "https://www.google.com"
}
}
Note: I have not used "notification" in the above payload, if you use the below payload while using the notification keyword in your payload it will open the app not the URL,
{
"to": "device-token",
"priority":"high",
"notification":{
},
"data":{
"body": "there",
"title": "Hi",
"LinkUrl": "https://www.google.com"
}
}
Cheers!

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