Working with Firebase Push Notifications without Notification channels

假如想象 提交于 2019-12-07 21:51:01

问题


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.


回答1:


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)



回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/49798229/working-with-firebase-push-notifications-without-notification-channels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!