52

How to set default notification channel for notification messages that come when an app is in the background? By default, these messages use "Miscellaneous" channel.

Maksim Ostrovidov
  • 10,720
  • 8
  • 42
  • 57

3 Answers3

66

As you can see here in the official docs, you need to add the following metadata element in your AndroidManifest.xml within the application component:

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

This default channel will be used when notification message has no specified channel, or if the channel provided has not yet been created by the app.

Louis CAD
  • 10,965
  • 2
  • 39
  • 58
Maksim Ostrovidov
  • 10,720
  • 8
  • 42
  • 57
  • 26
    where is this **default_notification_channel_id** – Mahesh Gawhane Jan 12 '18 at 06:42
  • 18
    in your strings.xml file, put `Channel ID`, you can take a look at this post for more information: https://medium.com/exploring-android/exploring-android-o-notification-channels-94cd274f604c – chornge Mar 13 '18 at 17:23
  • 4
    What happens if we do not create even a default notification channel like above in the app manifest? – user1261913 May 07 '19 at 06:24
  • 2
    @user1261913 I don't think setting your own default notification channel is necessary. The docs state: "FCM provides a default notification channel with basic settings. **If you prefer** to create and use your own default channel, set default_notification_channel_id to the ID of your notification channel object as shown;" – M165437 Jun 14 '19 at 22:41
  • 16
    Surely the string "Channel ID" defined in the strings res must tie up with SOMETHING SOMEWHERE? – The Tahaan Oct 25 '19 at 07:33
  • 3
    If creating the `strings.xml` file, be sure you format it correctly with the `xml version` and `resources` tag or you'll get build errors. See here: https://developer.android.com/guide/topics/resources/string-resource – Sludge Aug 05 '20 at 18:58
  • 2
  • You may refer to this post regarding how to create a notification channel in Flutter. https://stackoverflow.com/a/67543703/11204120 – luke77 May 15 '21 at 06:00
  • where this channel is checked? like the NotificationChannel.DEFAULT_CHANNEL_ID – Pavel Poley Jun 16 '21 at 10:15
  • Worked for me for the debug build, but doesn't seem to work in release. Why is that? – Henrique Guimarães Nov 07 '22 at 12:44
3

You need to have registered a channel using NotificationManager CreateNotificationChannel.

This example uses c# in Xamarin, but is broadly applicable elsewhere

private void ConfigureNotificationChannel()
{
    if (Build.VERSION.SdkInt < BuildVersionCodes.O)
    {
        // Notification channels are new in API 26 (and not a part of the
        // support library). There is no need to create a notification
        // channel on older versions of Android.
        return;
    }

    var channelId = Resources.GetString(Resource.String.default_notification_channel_id);
    var channelName = "Urgent";
    var importance = NotificationImportance.High;

    //This is the default channel and also appears in the manifest
    var chan = new NotificationChannel(channelId, channelName, importance);

    chan.EnableVibration(true);
    chan.LockscreenVisibility = NotificationVisibility.Public;

    var notificationManager = (NotificationManager)GetSystemService(NotificationService);
    notificationManager.CreateNotificationChannel(chan);

}

This channel should be unique eg com.mycompany.myapp.urgent

You then add a reference to a string inside the application tag in the AndroidManifest.xml

<application android:label="MyApp" android:icon="@mipmap/icon">
    <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id" />
</application>

Finally, setup the string in strings.xml

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
    <string name="default_notification_channel_id">com.mycompany.myapp.urgent</string>
</resources>
James Westgate
  • 11,306
  • 8
  • 61
  • 68
1

you need to add Cordova config.xml

<widget 
... 
xmlns:android="http://schemas.android.com/apk/res/android" 
...>



 <platform name="android">
    <config-file target="AndroidManifest.xml" parent="application" mode="merge">
      <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id"/>
    </config-file>
    <config-file target="res/values/strings.xml" parent="/*">
      <string name="default_notification_channel_id">urgent_alert</string>
    </config-file>
</platform>
Choo Chu
  • 141
  • 1
  • 8