如何在 Android O 中设置 Firebase 通知 ChannelID?

2023-12-25

对于 API 级别 26,我们必须设置一个通道 id 作为参考。我学会了如何在没有 ChannelID 的情况下做到这一点,下面是我的 Firebase 消息传递设置代码。但现在对于新的 Android api 26 级别

NotificationCompat.Builder(this);

这不起作用,我应该添加对channelID的引用作为

NotificationCompat.Builder(this, channelID);

但我不知道该怎么做。 您能否帮助我了解如何为 Firebase 云消息传递创建通道 ID?

AndroidManifest.xml

    <service android:name=".MyFirebaseInstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"></action>
        </intent-filter>
    </service>

    <service android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"></action>
        </intent-filter>
    </service>

MyFirebaseInstanceIdService Java 类:

import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

    private static final String REG_TOKEN = "REG_TOKEN";

    @Override
    public void onTokenRefresh() {
        String recent_token = FirebaseInstanceId.getInstance().getToken();
        Log.d(REG_TOKEN, recent_token);
    }
}

MyFirebaseMessagingService Java 类

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        // Here I will set up a channel ID as "CH_ID_1" but I do not know how to do this.
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CH_ID_1");
        //****************************************************

        notificationBuilder.setContentTitle("FCM NOTIFICATION");
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());

    }
}

Gradle - 依赖项

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.firebase:firebase-core:16.0.0'
    implementation 'com.google.firebase:firebase-messaging:17.0.0'
}
apply plugin: 'com.google.gms.google-services'

我之前在应用程序中所做的就是在应用程序启动后立即初始化通知通道。所以我添加了一个init我的应用程序类中的函数,如下所示:

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

        NotificationChannel topicChannel = new NotificationChannel(
                Constants.NOTIFICATION_CHANNEL_TOPIC,
                Constants.NOTIFICATION_CHANNEL_TOPIC,
                NotificationManager.IMPORTANCE_DEFAULT);
        topicChannel.setDescription(Constants.NOTIFICATION_CHANNEL_TOPIC);

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

        List<NotificationChannel> notificationChannels = new ArrayList<>();
        notificationChannels.add(publicChannel);
        notificationChannels.add(topicChannel);
        notificationChannels.add(privateChannel);

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (mNotificationManager != null) {
            mNotificationManager.createNotificationChannels(notificationChannels);
        }
    }

然后在我的FirebaseMessagingService,我做了一个函数来获取channelId构建通知时:

    private String getChannelId(String source) {
        if (!TextUtils.isEmpty(source)) {
            if (source.contains(Constants.TOPIC_PREFIX)) {
                return (TextUtils.equals((TOPIC_PREFIX + Constants.NOTIFICATION_CHANNEL_PUBLIC), source)) ?
                        Constants.NOTIFICATION_CHANNEL_PUBLIC : Constants.NOTIFICATION_CHANNEL_TOPIC;
            } else {
                return Constants.NOTIFICATION_CHANNEL_PRIVATE;
            }
        } else {
            return Constants.NOTIFICATION_CHANNEL_PUBLIC;
        }
    }

这满足了我们的应用程序需要的三种类型的通知——公共、主题和私人。您可以自行指定您需要的频道。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Android O 中设置 Firebase 通知 ChannelID? 的相关文章

随机推荐

  • 部分语音识别

    我目前在使用 Microsoft 语音引擎进行语音识别方面玩得很开心 它的设置和使用 C 非常简单 并且可以用比我最初想象的更少的代码行来实现伟大的事情 现在 重点是我真的不知道如何处理这个问题 我知道引擎需要开始监听 然后你说一个单词或一
  • 资产路径中缺少指纹

    我们刚刚使用新的资产管道部署了 Rails 3 1 应用程序 在开发中一切看起来都很好 但在生产中所有资产图像都丢失了 部署时 我们遵循 Ruby on Rails Edge Guide 作为示例 我们的应用程序中的一个简单代码片段 ass
  • 当用户单击链接时,想要使用 jquery 在链接下方显示 div

    我有链接和一个div 我想当用户单击链接时在链接下方显示 div 我想通过代码将 div 定位在链接下方 这意味着 div 的顶部和左侧将根据链接的顶部和左侧位置通过代码设置 需要帮忙 a About a div a href About
  • 如何在 C# 中释放桌面应用程序的 VM/私有字节

    我有用 C 开发的桌面应用程序 应用程序使用的虚拟机大小非常高 我想向 pdf 文件添加水印 该文件有超过 10 000 页 确切地说是 10776 页 VM 大小增加 有时应用程序冻结或抛出内存不足异常 是否有解决方案可以在 C 中以编程
  • 如何在android中单击按钮时对EditText应用撤消和重做操作

    我正在尝试在向 EditText 写入或应用效果时应用撤消和重做操作 为此 我已经从这里下载了课程Link https code google com p android issues detail id 6458 c123然后我在我的应用
  • SAS:使用 PROC IMPORT 导入 .xlsx 时定义类型

    问题 使用 PROC IMPORT 时如何定义从 xlsx 文件导入的变量的变量类型 My work 我正在使用 SAS v9 4 据我所知 它是普通的 SAS 我没有 SAS ACCESS 等 我的数据如下所示 ID1 ID2 MONTH
  • 使用 argparse 将参数发送给 Python 脚本中的函数

    我处于一种奇怪的情况 我需要一个 Python 函数从脚本中运行 然后从我的主代码中调用该脚本 我想用subprocess模块 并知道如何使用它将参数传递给纯脚本 但问题是 我需要将参数传递给其中的嵌套 Python 函数 其中大部分是可选
  • 故意损坏文件系统

    我必须明确测试我的应用程序在尝试访问 NTFS 文件系统的某些损坏部分时会执行什么操作 我四处寻找类似的问题 但这些问题都是关于破坏特定文件 即破坏 xml 文件的语法等 我需要创建一个场景chkdsk http technet micro
  • java中同步ArrayList的正确方法

    我不确定这是否是同步我的正确方法ArrayList 我有一个ArrayList in queue这是从传入的registerInQueue功能 ArrayList
  • 有趣的事件“Dispose”行为

    我注意到我们的 NET WinForms 应用程序中的有趣行为 我们有一个 MDI 表单 其中添加了许多 MDI 子项 这些子窗体监听 广播 事件 该事件本质上是调用刷新自身 该事件在基类中声明 并在子窗体中添加侦听事件 我注意到 即使这些
  • 使用 PyCharm 在远程服务器上进行 Python 绘图

    我已经开始学习 Python 到目前为止我的设置一直遵循 在 Win10 64 位本地计算机上安装 Python 3 5 并使用 PyCharm 作为一个很棒的 IDE 一切正常 matplotlib 图表和其他可视化输出显示良好 调试工作
  • Android Studio 4.1.1 macOS 全屏错误

    将 Mac 更新到 Big Sur 后遇到问题11 0 1 所有插页式对话框窗口 设备管理器 首选项 SDK 选项等都不会在全屏模式下作为插页式 对话框打开 而是作为应用程序内的选项卡打开 然后 Android Studio 冻结并且没有响
  • 是否可以在 Rust 中使用运行时确定大小的堆栈分配数组?

    是否有相当于alloca在 Rust 中创建可变长度数组 我正在寻找与以下 C99 代码等效的内容 void go int n int array n 这不可能directly 因为支持它的语言中没有直接语法 话虽这么说 C99 的这个特殊
  • 我们可以在 K8S 的 POD 中执行容器吗?

    我正在将 docker 镜像放入 POD 中 我们可以使用 docker exec 执行 Docker 容器 同样 有没有办法在 POD 中执行容器来检查一些数据 有多种方法可以进入 Pod 中的 Kubernetes 容器 kubectl
  • Python:self 与 type(self) 以及类变量的正确使用

    当在Python中使用类变量时 可以通过 self 感谢引用 或 type self 直接 访问和 如果它是可变的 直接操作它 而不可变变量 例如整数 显然可以得到当您只使用 self 时 会被新实例对象遮蔽 那么 在处理 Python 类
  • GDB 找不到行号,而 objdump 可以

    我正在使用 DDD GDB 调试在 NintendoDS 上运行的自制游戏 该游戏使用 arm eabi gcc devkitARM release 32 4 5 1 构建 仅供参考 我上传了未剥离的 elf 二进制文件 文件不再托管 以防
  • 圆弧碰撞检测

    那么如何实现圆弧的碰撞检测呢 我是否必须使用 Box 2d 碰撞 或者我可以使用 Rectangle 或类似的东西以其他方式进行操作吗 顺便说一句 我讨厌 box2d 因为我不理解其中的大部分内容 所以如果有一个排除 box2d 的解决方案
  • Perl 脚本与单行脚本 - 正则表达式的功能差异

    我有一个 Perl 程序 它采用 STDIN 从另一个 bash 命令通过管道传输 bash 命令的输出相当大 大约有 200 行 我想采取entire输入 多行 并将其提供给单行 perl 脚本 但到目前为止我尝试过的任何方法都不起作用
  • ModuleNotFoundError:没有名为“google.appengine”的模块

    我想在 Windows 上用 python3 进行谷歌搜索 谷歌说明说他们支持 python3 并输入 gcloud topic init 以获取详细信息 但没有说没有 python2 7 的解释器 我是否必须安装 python2 7 才能
  • 如何在 Android O 中设置 Firebase 通知 ChannelID?

    对于 API 级别 26 我们必须设置一个通道 id 作为参考 我学会了如何在没有 ChannelID 的情况下做到这一点 下面是我的 Firebase 消息传递设置代码 但现在对于新的 Android api 26 级别 Notifica