How to specify Android notification channel for FCM push messages in Android 8
Asked Answered
A

3

34

Our app now has targetSdkVersion 26 (Android 8) and the app uses FCM push notifications.

As FCM documentation prescribes I updated the FCM client library to version 11.2.0:

dependencies {
     compile 'com.google.firebase:firebase-messaging:11.2.0'
}

With this FCM client library update the FCM notifications started to appear on Android devices. Good, but when app is in background it's system who processes the FCM message, so it uses the default Android notification channel named "Miscellaneous", which is not what we want (we have other notification channels and "Miscellaneous" sounds confusing in that list).

As FCM documentation says there is a way to specify default notification channel for FCM messages:

(Optional) Within the application component, metadata elements to set a default icon, color and notification channel (new in Android O) for notifications. Android uses these values whenever incoming messages do not explicitly set icon, color or notification_channel.

However there is no code sample shown (samples are shown only for icon and color). So I just found by googling a sample in Firebase Cloud Messaging Quickstart on github:

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel"
    android:value="@string/default_notification_channel_id"/>

But it does not work - FCM notifications still appear within the "Miscellaneous" channel. And I see in the logs:

W/FirebaseMessaging: Missing Default Notification Channel metadata in AndroidManifest. Default value will be used.

Of course, I tried to reinstall the app. Still having the issue.

Well, ideally there should be some way to specify notification channel(s) on back-end at the moment of sending the messages. The FCM dev console, which allows to test sending, now has such an option in UI:

enter image description here

And it works fine. However our back-end uses Java Amazon SNS API and I have no idea if that API allows to specify Android notification channel when sending a message (because it'a new Android feature, and Amazon needs time to adopt it). So setting a default notification channel in AndroidManifest.xml would be a valid workaround for now, but it does not work.

Apollonian answered 29/8, 2017 at 10:54 Comment(4)
Did you create the string value default_notification_channel_id ?Paleobiology
Of course, I did.Apollonian
In you post you write ".default_notification_channel", but it should be "..default_notification_channel*_id*"Prototrophic
FYI, answer to a related question.Rau
S
19

Look at docs: https://firebase.google.com/docs/cloud-messaging/http-server-ref

android_channel_id The notification's channel id (new in Android O).

The app must create a channel with this ID before any notification with this key is received.

If you don't send this key in the request, or if the channel id provided has not yet been created by your app, FCM uses the channel id specified in your app manifest.

Try to include android_channel_id in json you are about to post to fcm. I have no idea why manifest value is not working for you. Try to just add channel to your request, you should get the same effect as from Firebase Console.

Edit: I just realized you are asking for Amazon client integration. Maybe you are able to build json request manually then (I don't know much about Amazon services, sorry).

Studner answered 29/8, 2017 at 11:4 Comment(9)
Thanks a lot! I was able to use android_channel_id param via Amazon SNS console (just by adding it to JSON payload). This is even better than default notification channel in AndroidManifest.xml, so I am no longer bothered with why it did not work. :)Apollonian
how can i create android_channel_idSteelman
The cited link points to a section marked Legacy HTTP Server Protocol, but the main protocol section makes no mention of android_channel_idGreysun
@VitKhudenko may I ask what exactly you addded to your json file? thanksUniversalism
@rosualin, the android_channel_id param with the string id value which is specific to my application.Apollonian
What happens if we do not create any notification channel?I read that FCM creates "default notification channel with basic settings".Does that mean I do not have to create a basic notification channel in the app manifest?Harborage
Have you even read docs? There will be default channel created in this case.Studner
android_channel_id will cause bad request: instead, use channel_id credits to li2 answerCinereous
@Studner Kindly share any example code in json format of notification to elaborate your answer, will be very helpful to all of us. Thanks a lot.Tartlet
P
19

FCM has migrated to HTTP v1 API:

https://fcm.googleapis.com/v1/projects/{{projectId}}/messages:send

android_channel_id will cause bad request:

"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
  {
    "field": "message.notification",
    "description": "Invalid JSON payload received. Unknown name \"android_channel_id\" at 'message.notification': Cannot find field."
  }

The correct payload should be:

{
    "message": {
        "token": "{{deviceToken}}",
        "notification": {
            "body": "This is an FCM notification message hello 23",
            "title": "FCM Message",
            "image": "https://lumiere-a.akamaihd.net/v1/images/au_moviesshowcase_mulan_poster_r_2_54011055.jpeg?region=0,0,960,1420"
        },
        "android": {
          "notification": {
            "channel_id": "channel_id_1"
          }
        },
        "data": {
            "key1": "42",
            "key2": "sent by 21"
        }
    }
}

see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#resource:-message

Penneypenni answered 9/4, 2020 at 5:53 Comment(7)
@VitKhudenko But the problem is onMessageReceived won't be getting called when App is in the background if the payload contains notification part, which means you cannot put channel id in the payload if you need onMessageReceived to be called no matter App is in foreground or background. Totally have no idea how to make both of them working. LolPenneypenni
Hey li2, I have the exact same situation. Did you or somebody else got it working?Anagram
@GuilhermeRamos you can add an extra key "channel_id" in "data" part, then parse the data payload remoteMessage.data on onMessageReceived callback, then you're able to do whatever you want.Penneypenni
Yeah, that's ok. But what about the app is not "running", it's in the background?Anagram
Finally worked after hours of searching, Thanks, man :)Cinereous
Hey, did anyone find a solution?Materiel
A data only payload where you parse the fields however you want and set the channel to whatever you want, and then post the notification yourself, seems to be the answer. A data only payload should call onMessageReceived when in the foreground or the backgroundCrouton
B
12

Depend on this resource : https://firebase.google.com/docs/cloud-messaging/http-server-ref

Here Payload look like :

{
   "to":"$device_token"
   "notification":{
      "title":"Title",
      "body":"Here Body",
      "android_channel_id":"$channel_id", // For Android >= 8
      "channel_id":"$channel_id", // For Android Version < 8
      "image": "https://xxxxx.com/xxxxx.jpeg"
   },
   "data":{},
   "priority":"normal"
}
Blucher answered 9/9, 2021 at 8:11 Comment(2)
thats what am searching for , y'all guys , check this answer code comments , hes specify android_channel_id & channel_idAlbanian
That's what I'm looking for because If our app is in background state then channel_id will not work if our android version is greater than Android 8 (Oreo). We have to provide both things in order to get work push notification properly in backgroundEnrollee

© 2022 - 2024 — McMap. All rights reserved.