【问题标题】:Working with Firebase Push Notifications without Notification channels在没有通知通道的情况下使用 Firebase 推送通知
【发布时间】:2018-04-12 13:45:07
【问题描述】:

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

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

Notification.Builder (Context context)

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

NotificationCompat.Builder (Context context, String channelId)

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

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

【问题讨论】:

  • 不幸的是,这似乎是目前唯一的方法,在这种情况下,最好的办法可能就是为所有通知提供一个渠道
  • @markusian 你能解释我如何在答案中做到这一点吗?还有......如果我想在没有PUSH的情况下显示通知,如何处理这个问题,只需在用户按下按钮时通知栏上的通知。
  • 我不确定你所说的“没有推送的通知”是什么意思,你能举个例子吗?
  • @markusian 例如,我希望当用户触摸我的应用程序的一部分时,会显示通知。到目前为止,我一直使用 Notification.Builder(上下文上下文)创建一个通知并将其显示在通知栏上。你明白吗?
  • 您可以以完全相同的方式进行操作,但您需要为其创建一个频道。其背后的想法是您将通知分组到频道中,因此用户可以根据他想要接收的通知类型来禁用/启用某个频道。如果您考虑一下,那真是个好主意。

标签: android firebase firebase-cloud-messaging android-notifications notification-channel


【解决方案1】:

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

在 api

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)

【讨论】:

  • 谢谢你,但你没有回答我的问题。如果您想向您的应用程序的所有用户发送一般通知,并且您想接收所有已安装应用程序中的所有通知,会发生什么情况?或者,如果您想在用户按下应用程序中的按钮时在通知栏中创建一个简单的通知,会发生什么?如何显示那种通知?我的意思是......就像在 api 26 和通知渠道出现之前一样工作。
  • 只需像在 api 26 之前一样使用频道 ID 发送通知
【解决方案2】:

目前没有解决方法。通知通道最近被宣布(如果我没记错的话,最后一次 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;
    }
}

【讨论】:

  • PS:Build 版本的检查似乎有点。 (叹气)我知道。 :'( 但这是你必须学会​​在 Android 中使用的东西。
【解决方案3】:

我不知道这是否回答了这个问题。但是,任何低于 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 以下没有任何影响。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-23
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2017-05-21
    • 1970-01-01
    • 2011-06-19
    相关资源
    最近更新 更多