I use this plugin for my Xamarin Forms app(android).
Any event not fire when app is in background. Events work when the application is in the foreground. But app is background any event not fire, the app is just opening. But I need to pass the message parameters to the application. I'm not sure if this is a bug, maybe this is my mistake. I will be grateful for any help.
I use this api for send message:
https://fcm.googleapis.com/fcm/send
This is my payload:
{
"data": {
"id_serv" : "12",
"id_group" : "1",
"click_action":"Accept"
},
"notification" : {
"body" : "test",
"title" : "test"
},
"priority": "high",
"condition": "'g1' in topics"
}
I also subscribe to all available events - OnNotificationAction, OnNotificationOpened, OnNotificationReceived.
I also try to subscribe at the android level, but this also does not work. Any help?
When the app is in the background firebase use their notification system that way your code not called.
If you want that your code will be called you need to pass the data through the data object only and not use the notification object.
so your notification object need to be like that:
"data": {
"id_serv" : "12",
"id_group" : "1",
"click_action":"Accept",
"body" : "test",
"title" : "test"
},
"priority": "high",
"condition": "'g1' in topics"
}
More info here: FB Receive massages
i don't know anything about xamarin, but in firebase version above 16, you just got push notification data when app is running,
when app is killed: after click on notification, android open your first activity with bundle (extra data), and you must get this data and use them, so debug your app and check your first activity bundle after clicking on notification
Related
I am using PushNotifications Capacitor plugin and FCM to send the notifications, everything works fine until I want to send a Push Notification with both notification and data fields on Android.
What I want to do is to send a custom data with the notification and consume the notification on app resume. I am using PushNotifications.getDeliveredNotifications() method to get the notifications on resume, on iOS I get all the notifications with proper data, but on Android data property is replaced with the object below:
{
body: "My Body",
data: {
android.appInfo: "ApplicationInfo{809371c app}",
android.bigText: "My Body",
android.progress: 0,
android.progressIndeterminate: false,
android.progressMax: 0,
android.reduced.images: true,
android.showChronometer: false,
android.showWhen: true,
android.template: "android.app.Notification$BigTextStyle",
android.text: "My Body",
android.title: "My Title",
gameDndOn: false,
specialType: "",
topFullscreen: false,
groupSummary: false,
id: 0,
title: "My Title"
}
}
I see in the FCM docs for Android apps that if both notification and data fields are present and the app is in the background the notification lands in the system tray and data in extras of the intent.
I am getting proper data when I tap on the notification, but in my case I want to get that on resume, without tapping on the notification.
Is there any way to get to that data in that case?
You have to create the message in this format
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
},
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
}
Note : notification structure is critical for receiving a notification. You need to have title and body. This gets displayed in the notification drawer.
data can be customized as per your requirement. This will have the data that you need inside your app.
Suggestion, keep your payload small. Send data which is absolutely necessary and also safe to send over FCM.
Look at this part of the official documentation
For handling push notifications in capacitor, you should use the listeners. Here is the official docs way of doing it.
I'm using com.google.firebase:firebase-messaging:20.0.0 on Android. When I try sending a notification like the one below it always shows the normal title and body instead of title_loc_key and body_loc_key when they are presented in the app! The fun thing is that it's not the case in iOS. In iOS it will always try to first show the localized resources and if it couldn't find them, it will default to normal ones.
{
"to" : "f6_numko7IQ:APA91bFrTN0fmThFDeAFy2...",
"collapse_key" : "type_a",
"notification" : {
"title_loc_key": "resource_name_1",
"body_loc_key": "resource_name_2",
"title": "Default Title",
"body": "Default Body!"
}
}
Is this a known issue? Is there a get around for it?
The solution is to remove the "title" and "body" entries. I know it should work like on iOS but currently it's not.
{
"to" : "f6_numko7IQ:APA91bFrTN0fmThFDeAFy2...",
"collapse_key" : "type_a",
"notification" : {
"title_loc_key": "resource_name_1",
"body_loc_key": "resource_name_2",
}
}
For Android: Using the "v1" api from firebase remove "title" and "body" from root "notification: {....}" and add into the "android": {
"notification": {
"title_loc_key": "push_notification_title",
"body_loc_key": "push_notification_body"
}
},", should work.
But you need to keep in mind the following:
The push notification will be displayed by default by firebase SDK based on the com.google.firebase.MESSAGING_EVENT, when the app is in the background --> opened (the process is in the background) and Notification permission is allowed. Also, you need to be sure that your app has already a channel created with the default_channel_id you defined in the manifest file. For this case, you will not be notified of the onMessageReceived callback provided by "FirebaseMessagingService".
When the app is in the foreground you will not receive any firebase notification, visible on the screen. Based on the Channel you have created at point 1) you need to trigger the notification display if required, by creating a NotificationBuilder .... and displaying it. This catch must happen inside the: onMessageReceived callback provided by "FirebaseMessagingService"
When the app is closed and notification is allowed the push notification will be displayed by default by firebase SDK based on the com.google.firebase.MESSAGING_EVENT. However, if the notification received by the device contains some data: {} payload that can be handled into the onCreate lifecycle method from activity by accessing the this.intent.extras. You will not be notified of the onMessageReceived callback provided by "FirebaseMessagingService".
If notification is disabled you will not get any push notification displayed visually but when the app is in the foreground you will be notified into the onMessageReceived callback provided by "FirebaseMessagingService". When the app is closed or in the background, you will get nothing.
Here I facing Notification issue in Android Oreo, That is when the FCM sent push notification to the Android Oreo, it will show the notification when the app is run in the foreground ,that is because of the service is running in the background , it can handle additional parameters and its value , but the problem arise when the app is close or paused state , The OS destroy the Service automatically when the app is closed or paused states , in order the handle notification FCM provides High priority Push notification method ,in which the push notification Json has two parts ,one is "data" , and another one is "notification", The data part can handle by the services when the app is running in foreground , but the Notification part manage by Android OS , There are default keys to handle notification for display notification icons ,title,messaged and Priority , With the use of high priority push notification can wake up the app when we hit on the notification ,and access the information by applying bundle with the starting Activity . but the problem is how to put extra data in In notification , when I trying to add additional data in to the "notification" part into the Json ,The OS not displaying notification . So how can I manage this issue .. ?
{
"to" : "e1w6hEbZn-8:APA91bEUIb2JewYCIiApsMu5JfI5Ak...",
"notification": {
"body": "message",
"title": "Title",
"icon": "appicon"
},
"data" : {
"body": "message",
"title": "Title",
}}
Try to add an extra parameter in notification object from server side manually. It worked for me.
{ "to" : "e1w6hEbZn-8:APA91bEUIb2JewYCIiApsMu5JfI5Ak...",
"notification": {
"body": "message",
"title": "Title",
"icon": "appicon" ,
"extra_param":"value"
},
"data" : {
"body": "message",
"title": "Title",
}
}
I have been trying to send the Push notification using FCM. It works completely when the app is in Foreground or background. But if i kill the app from recent list then i'm not getting any notification.
This is my structure of FCM with data payload
{"registration_ids":["here is my FCM token"],
"data":{
"body" :"Hello All, Hope you had a great time!",
"title":"Title",
"click_action":"ACTIVITY_MESSAGE",
"sound":"default",
"type" : "message",
"message": "message_here",
"time" : "2016-12-14 03:37:pm"
}
}
This is the structure with data and notification
{"registration_ids":["Here is my FCM token"],
"notification":{
"body" : "Hello All, Hope you had a great time!",
"title":"title here",
"click_action":"ACTIVITY_HOME",
"sound":"default"
},
"data":{
"type" : "message",
"message": "message_here",
"time" : "2016-12-14 03:37:pm"
}
}
I have also tried the same with notification tag and data as well but nothing is working in case whent the app is killed. If any one have any idea how can i get the notification in case of my app is killed then do share.
You should try to put "forceStart": "1" inside the data brackets.
In this way you should force the application to start the notification.
Is there a way to retrieve the key "message" on the device when sending notification to GCM.
{ "collapse_key" : "score",
"data" : { "k1" : "v1" },
"message" : "Hello World!",
"registration_ids" : [ "DEVICE_REGISTRATION_ID_GOES_HERE" ]
}
From the intent.getExtras(), I'm able to receive the following keys "collapse_key", "from", "k1" and their corresponding values as well. However I do not get key "message". If I do not add anything in the "data" dictionary, I still get push notification without "k1" (as expected).
Push to GCM does not result in errors. Does GCM drop keys not mentioned in "data" dictionary?
Yes the way you have it won't work. Anything you want to add, make it part of the data object:
{
"collapse_key": "score",
"data": {
"k1": "v1",
"message": "Hello World!"
},
"registration_ids": [
"DEVICE_REGISTRATION_ID_GOES_HERE"
]
}