Problem description
When I try to send a Notification
in Android O I have to specify a NotificationChannel
to send to.
If I use the old method (not setting up any channel) like this NotificationCompat.Builder(this)
, the Notification
will not be displayed.
The same is for an invalid channel like this NotificationCompat.Builder(this, "invalid")
or NotificationCompat.Builder(this, "")
.
When I send a notification through Firebase Cloud Messaging and have my application in the background with no notification channel specified, it will be a notification in the "Miscellaneous" channel.
When I try to do the same in the foreground above mentioned will not work and neither will creating a notification channel with the name "Miscellaneous" and id "{package}.MISCELLANEOUS" and then sending through it. When I do that what happens is the following:
How do I send a notification without a channel like FCM does it, so it lands in the regular "Miscellaneous" channel?
As I mentioned above, it happens with the FCM notifications, but e.g. Gmail also uses the miscellaneous channel. So how do I use it?
I believe that they would have removed the miscellaneous channel if it is normally unusable.
Why is this code not sending a notification to the "Miscellaneous" notification channel, it is in fact not sending any notification (only on Android O, the code works on lower Android versions).
(getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(1, NotificationCompat.Builder(this, NotificationChannel.DEFAULT_CHANNEL_ID)
.setSmallIcon(R.drawable.small_icon)
.setContentTitle(URLDecoder.decode("Title", "UTF-8"))
.setContentText(URLDecoder.decode("Text", "UTF-8"))
.setColor(ContextCompat.getColor(applicationContext, R.color.color))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT))
.build())
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() .
Notification channels are a new feature in Android Oreo that give users more control over the notifications they receive. Starting in Android Oreo, every local and push notification must be assigned to a single channel.
ChannelId is a unique String to identify each NotificationChannel and is used in Notification.
Notification categories are an Android Oreo (8.0+) feature which gives users finer control over notifications. When the user receives a notification with a category they can long press on it or go into the Notification Settings to change the category's settings.
The ID for that channel is fcm_fallback_notification_channel
. The firebase-messaging library creates it internally.
https://github.com/firebase/firebase-android-sdk/blob/076c26db27dd54d809fb2ccff8593b64fb3db043/firebase-messaging/src/main/java/com/google/firebase/messaging/CommonNotificationBuilder.java#L66
As it has been said in another answer, the id of the default channel created by Android System is fcm_fallback_notification_channel
, but be careful because the System doesn't create the channel until it has to manage the first push notification. So if you manage all notifications inside your extension of the FirebaseMessagingService
class, it may happen that the channel doesn't exist and you run into errors like:
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=fcm_fallback_notification_channel pri=-2 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
My recommendation is to check if the default channel exists before creating the notification and create it if it doesn't exist:
private void createDefaultNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = getSystemService(NotificationManager.class);
if (notificationManager.getNotificationChannel("fcm_fallback_notification_channel") != null) {
return;
}
String channelName = getString(R.string.fcm_fallback_notification_channel_label);
NotificationChannel channel = new NotificationChannel("fcm_fallback_notification_channel", channelName, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
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