Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 653
Maksim Ostrovidov Avatar asked Sep 05 '17 05:09

Maksim Ostrovidov


People also ask

What does default notification channel mean?

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. Follow this answer to receive notifications.

What is Channel ID in Android notification?

ChannelId is a unique String to identify each NotificationChannel and is used in Notification. Builder (line 7) when constructing the Notification object. NotificationChannel settings, except channel name and description text, are immutable after it is submitted to NotificationManager at line 5.

Do Firebase push notification or one signal in your Android app?

While both Firebase and OneSignal can be used to send mobile push notifications, the two platforms have many key differences. Here's an overview of some of them. Deliver all types of Android notifications with accurate previews.

How do you handle notifications when an app is in foreground in Firebase?

Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you'll need to write code to handle the onMessageReceived callback.


2 Answers

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.

like image 96
Maksim Ostrovidov Avatar answered Oct 02 '22 10:10

Maksim Ostrovidov


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> 
like image 35
James Westgate Avatar answered Oct 02 '22 09:10

James Westgate