Maksim Ostrovidov
Maksim Ostrovidov

Reputation: 11058

Firebase: How to set default notification channel in Android app?

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.

Upvotes: 56

Views: 96173

Answers (4)

rooxane.mac
rooxane.mac

Reputation: 131

as my cases i already added

  • metadata on AndroidManifest.xml
  • Android Notification Channel on MainActivity.kt
  • <string name="default_notification_channel_id" translatable="false">fcm_default_channel</string> on values/string.xml

then i figured out that my running devices at that time is on Night/Dark mode, so i need to copy values/string.xml to values-night/string.xml (create one if doesn't exist) then the local notifications will appear

so between the light and dark mode we need to adjust independently for the values

Upvotes: 0

James Westgate
James Westgate

Reputation: 11444

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>

Upvotes: 6

Choo Chu
Choo Chu

Reputation: 141

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>

Upvotes: 1

Maksim Ostrovidov
Maksim Ostrovidov

Reputation: 11058

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.

Upvotes: 71

Related Questions