Flutter: How to define notification channel in firebase_messaging package? - android

I am sending this json to cloud messaging (with proper headers) and want to get notification on my device, but it does not appear on my phone. I think that maybe i am missing channel id initialization for Android ?
*Additional info
If i send "notification" payload with title or body it is receiving, but i dont want to use "notification" payload. I want to have "data" payload only
{
"to" : "my device token",
"mutable_content" : true,
"content_available": true,
"data" : {
"content": {
"id": 100,
"channelKey": "basic_channel",
"title": "Huston!\nThe eagle has landed!",
"body": "A small step for a man, but a giant leap to Flutter's community!",
"notificationLayout": "BigPicture",
"largeIcon": "https://media.fstatic.com/kdNpUx4VBicwDuRBnhBrNmVsaKU=/full-fit-in/290x478/media/artists/avatar/2013/08/neil-i-armstrong_a39978.jpeg",
"bigPicture": "https://www.dw.com/image/49519617_303.jpg",
"showWhen": true,
"autoCancel": true,
"privacy": "Private"
},
"actionButtons": [
{
"key": "REPLY",
"label": "Reply",
"autoCancel": true,
"buttonType": "InputField"
},
{
"key": "ARCHIVE",
"label": "Archive",
"autoCancel": true
}
]
}
}

In order to define a notification channel you have to go to your AndroidManifest.xml and within the application component add this meta:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="#string/default_notification_channel_id"/>
then in your strings.xml file put the channel id like this:
<string name='default_notification_channel_id'>Channel ID</string>
After doing this, you can define your channel id in the FCM Console.

As simple as this. Just put your android channel name here below and it works with a charm:
"notification": {
"body": "Test notification",
"title": "Test Test Test",
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"sound": "your_custom_sound"
"android_channel_id": "channel_id_youcreated",
},
'to':
"",
},
Sound file name is not necessary in the above given notification payload if you assigned that sound to your notification channel thru MainActivity.kt or java file. However, it is necessary for older Android versions as they will use the sound file directly. Put your file inside res/raw folder.

Related

When I removed "click_action": "FCM_PLUGIN_ACTIVITY" then Ionic 1 app's push brakes

Firebase API push payload from backend :
{
"registration_ids": [
"IKSiok9Zpip5cDcebQ67wc7TnpDuhMAFJm1xdGGI44s48JbXEu5iYa"
],
"notification": {
"body": "Push Test Facility Category",
"title": null,
"icon": "myicon",
"sound": "mySound",
"vibrate": 1,
"Badge": 1,
"click_action": "FCM_PLUGIN_ACTIVITY"
},
"data": {
"message": "Push Test Facility Category",
"title": null,
"b": "22",
"id": "2",
"category_name": "Restaurants",
"h": "1",
"p": 1,
"type": "2"
}
}
fcm.service.ts
fcmListeners() {
this.firebase.onNotificationOpen().subscribe(async (data: any) => {
if (data.tap) {// when user tapped the background notification
} else { // Received in foreground
}
});
}
Can you tell me why the app doesn't open when I used the above payload and app is in the background? This is the behavior on an Android device. But no issues on iOS device.
If I use a firebase console cloud message (i.e. dashboard) then no issues there. i.e. it opens the app even in the background on android device. Any clue?
Note: I use the firebase plugin here.
Update: When I removed this "click_action": "FCM_PLUGIN_ACTIVITY" then it works fine on Ionic 4 apps. But it is not working on Ionic 1 app. Any clue?
Ionic 1: It works with this "click_action": "FCM_PLUGIN_ACTIVITY".
Ionic 4: It works without this "click_action": "FCM_PLUGIN_ACTIVITY"

How to config Firebase Push notification Message

I have found various examples on how to configure the message json for push notifcations on firebase for android and ios. But I have a problem, that not all push messages arrives to my client apps. I have noticed that on different android versions it work in a different way. Some of them, doesn't receive any message, some of them without the configured ringtone, some of them only with the default ringtone and some works fine. On client side I think every thing should be fine. I'am using xamarin forms for that. My message json looks like this. I have read now that I should remove the notification tag but it works then for older androids?
{
"to": "/topics/MYTOPIC",
"notification": {
"title": null,
"body": "test",
"sound": "de900",
"content_available": true
},
"priority": "high",
"data": {
"missionGuid": "",
"eventGuid": "",
"messageGuid": "e3ab4c34-125b-4ea7-abf7-3ee8fe1453ce",
"ric": "199900",
"title": null,
"body": "test",
"priority": "high",
"sound": "de900"
},
"android": {
"priority": "high",
"notification": {
"title": null,
"body": "test",
"sound": "de900",
"content_available": true
},
"data": {
"missionGuid": "",
"eventGuid": "",
"messageGuid": "e3ab4c34-125b-4ea7-abf7-3ee8fe1453ce",
"ric": "199900",
"title": null,
"body": "test",
"priority": "high",
"sound": "de900"
},
},
"apns": {
"headers": {
"apns-priority": 10
},
"payload": {
"aps": {
"alert": {
"title": null,
"body": "test"
},
"sound": "de900"
}
}
}
}
Setting the "priority" and "sound" in the notification payload never helped me. The behavior of this was very strange.
I decided to use only Data payload and handle both background/foreground messages inside onMessageReceived method and show your custom notifications.
After this I was able to show the notifications with the highest priority, pop-ups and sound.
Read more about Firebase data messages here
As far as I know, setting "priority": "high" is the only thing you can do to make sure the notifications deliver immediately. If they still can't receive anything, it's probably something on the client's side.
For the ringtone, supposedly, "sound": "WHATEVER" is the supposed to specify the sound, but again, because of the different OEMs and system variations, it could be more consistent to just send a data message and push out the notification yourself upon receiving, but you should know that even that probably won't guarantee either deliver or ringtone.
The fact that on some devices this works fine (hopefully the majority of them), indicates you probably have a correct set up for the configuration, and the best you can do is to handle the notification yourself and maybe have special cases for specific devices.

Android GCM Notification Arriving On Device But Not Displayed

I'm firing a message at GCM and it arrives on my device since I see logging on Android Studio Console.
The message as it goes out from my server has the format:
{
"to": "fsfsdfsdfsadf",
"notification": {
"title": "Alert Title",
"body": "Alert body with detailed description.",
"icon": "#drawable/ic_launcher"
},
"data": {
"title": "custom",
"user-id": "123",
"user-age": "44",
"parameters": [{
"parameter": "1"
}]
}
}
I have a very similar thing on iOS where the displaying of the notification is handled by the OS and then when a user taps on the notification a method is called where I do further processing, namely I use the 'data' key.
{
"aps": {
"alert": {
"title": "Alert Title",
"body": "Alert body with detailed description."
},
"content-available": false
},
"data": {
"title": "custom",
"user-id": "123",
"user-age": "44",
"parameters": [{
"parameter": "1"
}]
}
}
My understanding, perhaps incorrectly, is that if I include the 'notification' key in my message then Android will display the notification in the System Tray; just like on iOS the operating system will display the notification if you include the 'alert' key.
Am I correct?
Notification messages are delivered to the notification tray when the app is in the background. For apps in the foreground, messages are handled by these callbacks, onMessageReceived() in case of android
If you are app is in foreground you need to handle the display of notification, OS will only display the notification if app is in background. Refer to the official documentation for more details.

Multiple push notifications using ionic push

When sending two notifications the second one always overrides the first one in the notification center.
According to the docs this should happen only when using the tag:
"tag": Indicates whether each notification message results in a new
entry on the notification center. If not set, each request creates a
new notification. If set, and a notification with the same tag is
already being shown, the new notification replaces the existing one.
However this happens to me all the time, regardless whether I set a value for the tag or not.
I even tried setting a random tag & collapse_key (which as far I understand shouldn't be related in this case, but I gave it a try). Still didn't help.
Here's a sample of a notification object I'm sending:
{
"tokens":[
"my-device-token"
],
"profile":"my-profile-tag",
"notification":{
"message":"message",
"android":{
"payload":{
"collapse_key":9918519,
"tag":2825928
}
}
}
}
I resolved this problem.
You need to add "notId" into "data" in "android".
Example:
"notification": {
"title": "testing stack notification ",
"message":"is it working",
"android": {
"data": {
"title": "testing stack notification",
"message": "is it working",
"style": "inbox",
"summaryText": "yes its %n% notifications",
"notId": "123456"
}
}
}
You need to specify a unique notification id (unix timestamp for example) in the object in order not to overwrite the previous one - in your case:
{
"tokens":[
"my-device-token"
],
"profile":"my-profile-tag",
"notification":{
"notId": <some-unique-value-here>,
"message":"message",
"android":{
"payload":{
"collapse_key":9918519,
"tag":2825928
}
}
}
}

GCM push notification message outside data dictionary

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"
]
}

Categories

Resources