gpt4 book ai didi

android - NotificationCompat 与 API 26

转载 作者:IT王子 更新时间:2023-10-28 23:40:19 26 4
gpt4 key购买 nike

我没有看到任何关于如何将 NotificationCompat 与 Android O 的 Notification Channels

一起使用的信息

我确实看到了一个采用 channelId 的新构造函数,但是如何获取 Compat 通知并在 NotificationChannel 中使用它,因为 createNotificationChannel 采用 NotificationChannel对象

最佳答案

仅当 API >= 26 时才创建 NotificationChannel

public void initChannels(Context context) {
if (Build.VERSION.SDK_INT < 26) {
return;
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("default",
"Channel name",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Channel description");
notificationManager.createNotificationChannel(channel);
}

然后只需使用:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "default");

因此,您的通知适用于 API 26(带 channel )和更低版本(不带)。

关于android - NotificationCompat 与 API 26,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44443690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com