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:
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.
To create a notification channel, follow these steps: Construct a NotificationChannel object with a unique channel ID, a user-visible name, and an importance level. Optionally, specify the description that the user sees in the system settings with setDescription() .
ChannelId is a unique String to identify each NotificationChannel and is used in Notification.
Android Push Notification Channels were introduced in Android Oreo (Android 8.0 / API Level 26) to provide a unified system to help users manage the notifications. Users can now change preferences for an individual notification channel ( like opt-outs, vibration etc.)
Android Push notification using FCM (Firebase Cloud Messaging) 1 FCM (Firebase Cloud Messaging) Project setup on Firebase Console. One popup will appear. 2 Add FCM (Firebase Cloud Messaging) to Android app. Copy google-services.json file, go to your Android Project and paste it in app folder. ... 3 Send your first notification message. ...
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:
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.
Follow the complete article to implement an example of FCM. To add firebase to the project please refer Adding Firebase to Android App. The following is the gist of adding FCM to the app. Go to Tools -> Firebase -> Cloud Messaging -> Set up Firebase Cloud Messaging
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).
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
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"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With