通知栏,使用自定义布局

通知栏添加自定义布局 , 一定要注意

添加channel, 还有构建的时候把id加进去。

String channelId = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    channelId = "1";
    NotificationChannel channel = new NotificationChannel(channelId, "Channel1", NotificationManager.IMPORTANCE_DEFAULT);
    channel.enableLights(true); //是否在桌面icon右上角展示小红点
    channel.setLightColor(Color.RED); //小红点颜色
    channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
    mNotificationManager.createNotificationChannel(channel);
}
// channelId非常重要,不设置通知栏不展示
// builder.setChannelId(context.packageName)

Notification notification = new NotificationCompat.Builder(getContext(), channelId)
        .setContentTitle("通知2") // 创建通知的标题
        .setContentText("这是第二个通知") // 创建通知的内容
        .setSmallIcon(R.drawable.icon_app) // 创建通知的小图标
        .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                R.drawable.icon_app)) // 创建通知的大图标
        /*
         * 是使用自定义视图还是系统提供的视图,上面4的属性一定要设置,不然这个通知显示不出来
         */
        .setDefaults(Notification.DEFAULT_ALL)  // 设置通知提醒方式为系统默认的提醒方式
        .setContent(getRemoteView()) // 通过设置RemoteViews对象来设置通知的布局,这里我们设置为自定义布局
        .setChannelId(channelId)//channelId非常重要,不设置通知栏不展示
        .build(); // 创建通知(每个通知必须要调用这个方法来创建)

mNotificationManager.notify(2, notification); // 发送通知
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值