creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126734

How to send to "Miscellaneous" notification channel?

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:

Screenshot from my app's settings

What I want to know

How do I send a notification without a channel like FCM does it, so it lands in the regular "Miscellaneous" channel?

Examples of this working

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?

Screenshot of Gmail's notification channels

I believe that they would have removed the miscellaneous channel if it is normally unusable.

Shorter description

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())

Upvotes: 20

Views: 10570

Answers (2)

guest
guest

Reputation: 6698

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

Upvotes: 16

Sca09
Sca09

Reputation: 381

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);
}

Upvotes: 8

Related Questions