Android 通知不显示?

看书的时候,按照代码写一个通知,万万没想到,竟然显示不出来,我的天,My Cod !

手机坏了?  What ?

经过一份搜索,原来android 8.0之后通知需要加入channelId元素,否则无法显示通知,老衲手机是Android 9 (哼,趁我不注意自己升级,差评);

这就郁闷了,还是记录一下,下次遇到方便查找。

实现通知的示例代码(兼容Android O):

/**

* 显示通知

*/

private void showFoldNotification() {

NotificationCompat.Builder notificationCompatBuilder =

new NotificationCompat.Builder(getApplicationContext(),

Objects.requireNonNull(createNotificationChannel(this)));

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));

PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

Notification notification = notificationCompatBuilder

.setContentTitle("标题")

.setContentText("内容")

.setSmallIcon(R.mipmap.ic_launcher)

.setLargeIcon(BitmapFactory.decodeResource(

getResources(),

R.mipmap.ic_launcher))

.setContentIntent(pi)

.setDefaults(NotificationCompat.DEFAULT_ALL)

.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))

.setWhen(System.currentTimeMillis())

.setPriority(NotificationCompat.PRIORITY_DEFAULT)

.setAutoCancel(true)

.build();

NotificationManagerCompat.from(getApplicationContext()).notify(1, notification);

}

代码段 小部件

/**

* 删除通知渠道

*/

NotificationChannel mChannel = mNotificationManager.getNotificationChannel(id);

mNotificationManager.deleteNotificationChannel(mChannel);

详谈通知的基本使用:

创建通知:

获取NotificationManagers实例,管理通知:

NotificationManager notificationManager =

(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

创建NotificationCompat.Builder对象

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), channelId);

为Notification设置内容:

.setContentTitle("标题") //设置通知标题

.setContentText("内容") //设置通知内容

.setSmallIcon(R.mipmap.ic_launcher) //设置通知的小图标

.setLargeIcon(BitmapFactory.decodeResource(

getResources(),

R.mipmap.ic_launcher)) //设置通知图标

.setWhen(System.currentTimeMillis()) //设置时间

.setContentIntent(pi) // 设置点击通知时的意图

.setDefaults(NotificationCompat.DEFAULT_ALL) //打开呼吸灯,声音,震动(下方详解)

.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary)) //设置通知栏颜色

.setCategory(Notification.CATEGORY_REMINDER) //设置通知类别

.setPriority(NotificationCompat.PRIORITY_DEFAULT) //设置优先级,级别高的排在前面

.setVibrate(new long[]{100,200,300,400}) //设置震动

.setLight(Color.Red,500,500) //设置呼吸灯效果,一般三种颜色:红绿蓝

.setAutoCancel(true); //设置通知点击后图标消失

创建NotificationChannel :

public static String createNotificationChannel(Context context) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

channelId:全局唯一,长度不能过长

channelName:渠道名称,显示在通知栏列表

channelDescription: 设置描述 最长30字符

importance:重要等级,等级不同在手机桌面上展示的顺序不同

NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, channelImportance);

notificationChannel.setDescription(channelDescription);

// 允许通知使用震动,默认为false

notificationChannel.enableVibration(true);

// 设置显示模式

notificationChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);

NotificationManager notificationManager =

(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.createNotificationChannel(notificationChannel);

return channelId;

} else {

return null;

}

}

删除渠道通知:

NotificationChannel mChannel = mNotificationManager.getNotificationChannel(id); mNotificationManager.deleteNotificationChannel(mChannel);

看书的时候,按照代码写一个通知,万万没想到,竟然显示不出来,我的天,手机坏了?  经过一份搜索,原来android 8.0之后通知需要加入channelId元素,否则无法显示通知,这就郁闷了,还是记录一下,下次遇到方便查找。

NotificationChannel 的方法列表:

getId() —  获取 ChannleId

enableLights() —  开启指示灯,如果设备有的话

setLightColor() —  设置指示灯颜色

enableVibration() —  开启震动

setVibrationPattern() —  设置震动频率

setImportance() —  设置频道重要性

getImportance() —  获取频道重要性

setSound() —  设置声音

getSound() —  获取声音

setGroup() —  设置 ChannleGroup

getGroup() —  得到 ChannleGroup

setBypassDnd() —  设置绕过免打扰模式

canBypassDnd() —  检测是否绕过免打扰模式

getName() —  获取名称

setLockScreenVisibility() —  设置是否应在锁定屏幕上显示此频道的通知

getLockscreenVisibility() —  检测是否应在锁定屏幕上显示此频道的通知

setShowBadge() 设置是否显示角标

canShowBadge() —  检测是否显示角标

NotificationChannel中importance:

IMPORTANCE_NONE = 0; 不提示,不展示

IMPORTANCE_MIN = 1; 不提示,在通知下拉栏会展示,但是是收起的

IMPORTANCE_LOW = 2; 会在状态栏中显示,但不会弹窗,通知下拉栏会展示

IMPORTANCE_DEFAULT = 3; 会在状态栏中显示,允许有声音提示,但不会弹窗,通知下拉栏会展示

IMPORTANCE_HIGH = 4; 会弹窗提示,允许有提示音

IMPORTANCE_MAX = 5; 会弹窗提示,允许有提示音,可以使用全屏

Notification.Builder中的setDefaults:

setDefaults(NotificationCompat.DEFAULT_VIBRATE) ;    // 添加默认震动提醒

setDefaults(NotificationCompat.DEFAULT_SOUND) ;     // 添加默认声音提醒

setDefaults(NotificationCompat.DEFAULT_LIGHTS) ;     // 添加默认闪光灯提醒

setDefaults(NotificationCompat.DEFAULT_ALL) ;           // 添加默认以上三种提醒

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐