安卓8.0以上的通知栏适配


```java
## 安卓8.0以上的通知栏适配
项目中遇到的问题,此贴留做记录。
 Intent intent = new Intent(BackService.this, LoadingActivity.class);
                            PendingIntent pendingIntent = PendingIntent.getActivity(BackService.this, 0, intent, 0);
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                Notification notification;
                                Notification.Builder builder;
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                    builder = new Notification.Builder(BackService.this, "5996773");
                                } else {
                                    builder = new Notification.Builder(BackService.this);
                                }

                                //设置标题
                                builder.setContentTitle("我是标题")  //显示通知的标题
                                        .setContentText("我是内容")//显示消息通知的内容
                                        .setWhen(System.currentTimeMillis())//显示通知的具体时间
                                        .setSmallIcon(R.mipmap.ic_launcher)//这里设置显示的是手机顶部系统通知的应用图标
                                        .setAutoCancel(false)//点击通知后通知消失
                                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//这里设置显示的是下拉通知栏后显示的系统图标
                                        .setContentIntent(pendingIntent)
                                        //.setAutoCancel(true)//可以在此使用此方法,点击通知后,通知内容自动取消,也可以在NotificationActivity.java中设置方法取消显示通知内容
                                        .setVibrate(new long[]{0, 1000, 1000, 1000});//设置发出通知后震动一秒,停止一秒后再震动一秒,需要在manifest.xml中设置权限

                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                    NotificationChannel channel = new NotificationChannel("5996773", "安卓10a", NotificationManager.IMPORTANCE_DEFAULT);
                                    channel.enableLights(true);//是否在桌面icon右上角展示小红点
                                    channel.setLightColor(Color.GREEN);//小红点颜色
                                    channel.setShowBadge(false); //是否在久按桌面图标时显示此渠道的通知
                                    notificationManager.createNotificationChannel(channel);
                                }

                                notification = builder.build();
                                notificationManager.notify(1, notification);

                            } else {
                                Notification notification = new Notification.Builder(BackService.this)
                                        .setContentTitle("我是标题") //显示通知的标题
                                        .setContentText("我是内容")//显示消息通知的内容
                                        .setWhen(System.currentTimeMillis())//显示通知的具体时间
                                        .setSmallIcon(R.mipmap.ic_launcher)//这里设置显示的是手机顶部系统通知的应用图标
                                        .setAutoCancel(false)//点击通知后通知消失
                                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//这里设置显示的是下拉通知栏后显示的系统图标
                                        .setContentIntent(pendingIntent)
                                        //.setAutoCancel(true)//可以在此使用此方法,点击通知后,通知内容自动取消,也可以在NotificationActivity.java中设置方法取消显示通知内容
                                        .setVibrate(new long[]{0, 1000, 1000, 1000})//设置发出通知后震动一秒,停止一秒后再震动一秒,需要在manifest.xml中设置权限
                                        .build();
                                notificationManager.notify(2, notification);
                            }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值