通知集频道和 FCM subscribeToTopic 有什么不同?

Mor*_*ton 3 android android-notifications firebase firebase-cloud-messaging

我的应用有一个会员系统(会员类型包括 A?B?C),我使用 FirebaseFCM向我的应用推送通知。

如果用户使用 A 登录,我将使用FirebaseMessaging.getInstance().subscribeToTopic("A");unsubscribeFromTopic("B"); unsubscribeFromTopic("C");

它工作得很好,直到我发现如果设备的 API >=26 我的应用程序通知将不会显示。原因是设置频道是最新 android 版本的重要更新。

我已经更改了我的通知代码,就像这样:

// For API >=26
NotificationManager mNotificationManager =
        (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(
            "A",
            "A",
            NotificationManager.IMPORTANCE_HIGH);
    channel.setDescription("description");
    channel.enableLights(true);
    channel.enableVibration(true);

    Notification.Builder builder =
            new Notification.Builder(mContext)
                    .setSmallIcon(R.drawable.notification_icon)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setChannelId("A");
    mNotificationManager.notify(1, builder.build());
}
Run Code Online (Sandbox Code Playgroud)

现在,如果FCM从 A 推送,我可以显示通知。

显然,如果我想从 B 或 C 获得,那么我应该设置通道 B 或 C。

但是成员类型 A 应该只能从 A 获取,所以我找到了官方文档Delete a notification channel

NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "my_channel_01";
mNotificationManager.deleteNotificationChannel(id);
Run Code Online (Sandbox Code Playgroud)

我对此感到困惑,那么我的FCM subscribeToTopic("A");unsubscribeFromTopic("B"); unsubscribeFromTopic("C");

我应该编写另一个代码setChannel(id);并且deleteNotificationChannel(id);仅用于 API >=26 吗?

任何建议将不胜感激。提前致谢。

yos*_*rel 6

主题 是组消息的字符串标识符。当发送关于某个主题的通知时,所有注册到该主题的设备都会收到它。

新闻应用程序的开发人员可能会使用此机制来允许用户选择感兴趣的主题以获取警报,例如:天气、汽车、金融、犯罪等

有些应用定义了多个主题,用户自己可以订阅和取消订阅,有些应用使用这种机制通过定义主题向所有用户发送通知,并将所有用户(硬编码)注册到该主题。

通道 是一组设置,定义了当通知到达设备时用户如何收到警报。

Usually the developer would define the sound, vibration, light and more settings for each channel. User can always change those settings and even disable notification from specific channel.

A developer of a news app, might define a channel with vibration and high sound for breaking news, while creating a silent channel with low priority for promotions