Working with Firebase Push Notifications without Notification channels
Asked Answered
H

3

2

I need to generate notifications when a PUSH notification is received but also I need to generate notifications (for display them in the notification bar of the device) when something happens in the application, so I'm using NotificationCompat.Builder for it.

As you know, android has deprecated this call to Notification.Builder:

Notification.Builder (Context context)

And now you must use this call:

NotificationCompat.Builder (Context context, String channelId)

What happens if you don't want to specify a notification channel and you want to send general notifications to all the users of your app and you want to receive all the notifications in all the apps installed without dealing with notification channels? Or what happens if you want to create a simple notification in the notification bar when a user has pressed a button in your app? How to display a notification without specifying the channelId? I mean... just working like until api 26 and before notification channels appeared.

Can't see how to work without specifying notification channels in any place of the official documentation.

Halting answered 12/4, 2018 at 13:45 Comment(5)
Unfortunately this seems to be the only way right now, and in this case probably the best thing to do would be simply to have one channel for all your notificationsTabbie
@markusian can you explain me how to do that in an answer? also... how to deal with this problem if i want to display a notification without PUSH, simply a notification on the notification bar when the user press a button.Halting
I am not sure what you mean by a "notification without PUSH", can you provide an example?Tabbie
@markusian for example, I want that when the user touches a part of my app, a notification get's displayed. I did it until now creating one notification with Notification.Builder (Context context) and displaying it on the notification bar. Do you understand?Halting
You can do it in the exact same way, but you would need to create a channel for it. The idea behind it is that you group the notifications in channels, so the user could disable/enable a certain channel according to what kind of notifications he'd like to receive. If you think about it, that's a neat idea.Tabbie
A
0

Notification Channels are mandatory on Android 8+. So you must use NotificationCompat.Builder(Context context, String channelId) and create channel(s) on api 26+ via NotificationManager.createNotificationChannel(NotificationChannel channel).

On api < 26, just don't call createNotificationChannel but let the channel id parameter (just a String).

val builder = NotificationCompat.Builder(context, "a_channel_id")
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setSmallIcon(R.drawable.ic_notif)
            .setAutoCancel(true)
...
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.notify(NOTIFICATION_ID, builder.build())

on Api 26+, create a channel before:

val channel = NotificationChannel("a_channel_id", "channel_name", NotificationManager.IMPORTANCE_HIGH)
channel.description = "channel_description"
channel.enableLights(true)
channel.lightColor = Color.RED
channel.enableVibration(true)
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.createNotificationChannel(channel)
Anglosaxon answered 12/4, 2018 at 14:12 Comment(2)
Thank you but you are not answering my question. What happens if you want to send general notifications to all the users of your app and you want to receive all the notifications in all the apps installed? Or what happens if you want to create a simple notification in the notification bar when a user has pressed a button in your app? How to display that kind of notification? I mean... just working like until api 26 and before notification channels appeared.Halting
Just send your notification as you did before api 26 with a channel idAnglosaxon
P
0

There is currently no workaround for this. Notification Channels has been recently announced (last last I/O if I remember correctly), and is (most probably if not absolutely) here to stay. What I do though is something like this.

To abide to the new standard, I just implement the Notification Channels, but only as needed. I also use FCM on my app and here's something similar to what I have for it -- this is in my Application class:

    private void initFirebase() {
        ... // other Firebase stuff.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) initNotificationChannels();
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void initNotificationChannels() {
        NotificationChannel publicChannel = new NotificationChannel(NOTIFICATION_CHANNEL_PUBLIC,
                NOTIFICATION_CHANNEL_PUBLIC, NotificationManager.IMPORTANCE_DEFAULT);
        publicChannel.setDescription(NOTIFICATION_CHANNEL_PUBLIC);

        NotificationChannel privateChannel = new NotificationChannel(NOTIFICATION_CHANNEL_PRIVATE,
                NOTIFICATION_CHANNEL_PRIVATE, NotificationManager.IMPORTANCE_HIGH);
        publicChannel.setDescription(NOTIFICATION_CHANNEL_PRIVATE);

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (mNotificationManager != null) {
            mNotificationManager.createNotificationChannel(publicChannel);
            mNotificationManager.createNotificationChannel(privateChannel);
        }
    }

And my MessagingService has something like this:

private static final String NOTIFICATION_CHANNEL_PRIVATE = "my.app.package.name.private";
private static final String NOTIFICATION_CHANNEL_PUBLIC = "my.app.package.name.public";

private void buildNotification(....(other params),String source, String message) {
    String channelId = getChannelId(source);

    Intent resultIntent = new Intent(this, MyActivity.class);
    resultIntent.putExtra(EXTRAS_PARAM_ID, myVal);
    PendingIntent notificationIntent = buildNotificationIntent(channelId, roomId, roomType);

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, getChannelId(source))
                    .setSmallIcon(R.drawable.ic_sample
                    .setContentTitle(title)
                    .setContentText(message)
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .setContentIntent(notificationIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(id, 0, notificationBuilder.build());
}

private String getChannelId(String source) {
    switch(source){
        case PRIVATE:
           return NOTIFIFICATION_CHANNEL_PRIVATE;
        default:
           return NOTIFICATION_CHANNEL_PUBLIC;
    }
}
Promotive answered 12/4, 2018 at 14:39 Comment(1)
PS: The checking of the Build version seems kinda dirty. (sighs) I know. :'( But it's a thing you have to learn to go with in Android.Promotive
F
0

I don't know if this answers the question or not. But, having any channel below api 26 just worked without doing anything on my app.

1. instantiate notificationCompat with some channel Id 
//which is irrelevant for api < 26 

2. handle the case of creating notification channel for api 26+

3. bundled it up.

It just worked. Configuring Notifications did not have any effects below api 26.

Felicific answered 12/4, 2018 at 15:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.