我没有看到任何有关如何将NotificationCompat与Android O Notification Channels
我确实看到一个新的构造函数,它接受channelId
但是如何获取Compat通知并在NotificationChannel中使用它,因为createNotificationChannel
需要NotificationChannel
个对象
答案 0 :(得分:114)
仅在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(带通道)和低于(不带)。
答案 1 :(得分:14)
声明通知管理员:
final NotificationManager mNotific=
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence name="Ragav";
String desc="this is notific";
int imp=NotificationManager.IMPORTANCE_HIGH;
final String ChannelID="my_channel_01";
通知渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel mChannel = new NotificationChannel(ChannelID, name,
imp);
mChannel.setDescription(desc);
mChannel.setLightColor(Color.CYAN);
mChannel.canShowBadge();
mChannel.setShowBadge(true);
mNotific.createNotificationChannel(mChannel);
}
final int ncode=101;
String Body="This is testing notific";
通知构建器
Notification n= new Notification.Builder(this,ChannelID)
.setContentTitle(getPackageName())
.setContentText(Body)
.setBadgeIconType(R.mipmap.ic_launcher)
.setNumber(5)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setAutoCancel(true)
.build();
NotificationManager通知用户:
mNotific.notify(ncode, n);
答案 2 :(得分:1)
NotificationChannel实际上将多个通知分组到频道中。它基本上可以更好地控制用户的通知行为。您可以在Working with Notification Channel | With Example
了解有关通知渠道及其实施的更多信息通知频道仅适用于Android Oreo。
UserContext.User
请注意,传递给构造函数的Channel ID充当该Notification Channel的唯一标识符。现在创建通知,如下所示
//Notification channel should only be created for devices running Android 26
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel("unique_channel_id","channel_name",NotificationManager.IMPORTANCE_DEFAULT);
//Boolean value to set if lights are enabled for Notifications from this Channel
notificationChannel.enableLights(true);
//Boolean value to set if vibration is enabled for Notifications from this Channel
notificationChannel.enableVibration(true);
//Sets the color of Notification Light
notificationChannel.setLightColor(Color.GREEN);
//Set the vibration pattern for notifications. Pattern is in milliseconds with the format {delay,play,sleep,play,sleep...}
notificationChannel.setVibrationPattern(new long[]{500,500,500,500,500});
//Sets whether notifications from these Channel should be visible on Lockscreen or not
notificationChannel.setLockscreenVisibility( Notification.VISIBILITY_PUBLIC);
}
要向此频道添加任何通知,只需传递频道ID,如下所示
// Creating the Channel
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
答案 3 :(得分:0)
如果你做了所有工作并且没有得到任何结果,请小心。在某些设备上,您必须设置通知优先级。
final NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(mContext, "default")
.setPriority(Notification.PRIORITY_MAX);
答案 4 :(得分:0)
我知道这个答案来晚了,但是迟到总比不来好!
我刚刚发布了notification-channel-compat库,该库提供了从OS 4.0开始的Notification Channel支持。由于开发人员无论如何都必须针对通道进行设计,因此他们现在可以将通道的优势用于所有设备,而不必为较旧的设备单独设计。
该库对OS 8.0+设备使用内置的通道类,对于较旧的设备则模拟它。它所要做的就是使用我们的NotificationChannelCompat
,NotificationChannelGroupCompat
和NotificationChannelManagerHelper
类,并添加一行代码。您可以在github上看到更多内容。请对其进行测试,并让我知道任何issues。
谢谢
狮子座