2

我需要在收到 PUSH 通知时生成通知,但我还需要在应用程序中发生某些事情时生成通知(以便在设备的通知栏中显示它们),所以我正在使用NotificationCompat.Builder它。

如您所知,android 已弃用此对 Notification.Builder 的调用:

Notification.Builder (Context context)

现在你必须使用这个调用:

NotificationCompat.Builder (Context context, String channelId)

如果您不想指定通知渠道,并且想向应用程序的所有用户发送一般通知,并且希望在不处理通知渠道的情况下接收所有已安装应用程序中的所有通知,会发生什么情况?或者,如果您想在用户按下应用程序中的按钮时在通知栏中创建一个简单的通知,会发生什么?如何在不指定的情况下显示通知channelId?我的意思是......就像在 api 26 和通知渠道出现之前一样工作。

如果不在官方文档的任何地方指定通知渠道,就看不到如何工作。

4

3 回答 3

0

我不知道这是否回答了这个问题。但是,让任何低于 api 26 的频道都可以正常工作,而无需在我的应用程序上做任何事情。

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.

它刚刚奏效。配置通知在 api 26 以下没有任何影响。

于 2018-04-12T15:09:26.177 回答
0

通知渠道在 Android 8+ 上是强制性的。因此,您必须NotificationCompat.Builder(Context context, String channelId)通过 api 26+ 使用和创建通道NotificationManager.createNotificationChannel(NotificationChannel channel)

在 api < 26 上,不要调用 createNotificationChannel 而是让通道 id 参数(只是一个字符串)。

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

在 Api 26+ 上,在之前创建一个频道:

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)
于 2018-04-12T14:12:21.520 回答
0

目前没有解决方法。通知通道最近被宣布(如果我没记错的话,最后一次 I/O),并且(如果不是绝对的话,很有可能)会继续存在。我所做的就是这样。

为了遵守新标准,我只实现了通知通道,但仅在需要时。我还在我的应用程序上使用了 FCM,这与我所拥有的类似——这是在我的 Application 类中:

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

MessagingService有这样的东西:

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;
    }
}
于 2018-04-12T14:39:33.387 回答