android 生成前台通知渠道 NotificationChannel

本文介绍了从Android 8.0开始如何创建和管理前台服务通知渠道,包括自定义声音、震动以及设置通知重要性。示例代码展示了如何创建、删除通知渠道,并通过NotificationChannel设置不同级别的重要性和显示效果。此外,还分享了关闭已创建通知渠道的方法。
android 8.0系统开始加入了一个新功能,就是可以生成前台通知渠道,用于自定义声音,震动等快捷操作,如果想一直常驻于通知栏,在自定义的Service中调用
startForeground(notifyID, notification);

就可以了,代码例子如下:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            String id = "imservice";
            String name = "前台通知渠道测试";
            String description = "测试内容";
            int importance = 0;
            NotificationChannel mChannel = new NotificationChannel(id, (CharSequence) name, importance);
            mChannel.setSound((Uri) null, (AudioAttributes) null);
            mChannel.setDescription(description);
            notificationManager.createNotificationChannel(mChannel);
            int notifyID = 999;
            Notification notification = (new Notification.Builder((Context) this))
                    .setContentTitle((CharSequence) name)
                   
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值