Android8.0+ 通知适配实战代码

作者: Zachary46 | 来源:发表于2018-10-29 16:43 被阅读35次
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationManager notificationManager = (NotificationManager)
                    getSystemService(Context.NOTIFICATION_SERVICE);

            //分组(可选)
            //groupId要唯一
            String groupId = "group_001";
            NotificationChannelGroup group = new NotificationChannelGroup(groupId, "广告");
            String groupId2 = "group_002";
            NotificationChannelGroup group2 = new NotificationChannelGroup(groupId2, "新闻");

            //创建group
            notificationManager.createNotificationChannelGroup(group);
            notificationManager.createNotificationChannelGroup(group2);

            //channelId要唯一
            String channelId = "channel_001";
            NotificationChannel adChannel = new NotificationChannel(channelId,
                    "推广信息", NotificationManager.IMPORTANCE_DEFAULT);
            //补充channel的含义(可选)
            adChannel.setDescription("推广信息");
            //将渠道添加进组(先创建组才能添加)
            //adChannel.setGroup(groupId);
            //创建channel
            notificationManager.createNotificationChannel(adChannel);

            //channelId2要唯一
            String channelId2 = "channel_002";
            NotificationChannel adChannel2 = new NotificationChannel(channelId2,
                    "广告主题", NotificationManager.IMPORTANCE_DEFAULT);
            //补充channel2的含义(可选)
            adChannel2.setDescription("广告主题");
            //将渠道添加进组(先创建组才能添加)
            adChannel2.setGroup(groupId);
            //创建channel2
            notificationManager.createNotificationChannel(adChannel2);

            //channelId3要唯一
            String channelId3 = "channel_003";
            NotificationChannel adChannel3 = new NotificationChannel(channelId3,
                    "新闻资讯", NotificationManager.IMPORTANCE_DEFAULT);
            //补充channel3的含义(可选)
            adChannel3.setDescription("新闻资讯");
            //将渠道添加进组(先创建组才能添加)
            adChannel3.setGroup(groupId2);
            //创建channel3
            notificationManager.createNotificationChannel(adChannel3);

            Intent intent=new Intent(MainActivity.this,TiaoZhuanActivity.class);
            intent.putExtra("param","传递的参数");

            //参数:1:Context  2:一般不用 通常传入0  3:Intent  4:FLAG_CANCEL_CURRENT(),FLAG_NO_CREATE,FLAG_ONE_SHOT,FLAG_UPDATE_CURRENT
            //PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,0,intent,0);//延迟跳转
            int notifyId = (int) System.currentTimeMillis();
            PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, notifyId, intent, PendingIntent.FLAG_UPDATE_CURRENT);//PendingIntent.FLAG_UPDATE_CURRENT
            NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification=new NotificationCompat.Builder(MainActivity.this, channelId)//8.0以上没设置渠道Id无法显示通知的
                    .setContentTitle("紧急通知:")
                    .setContentText("点赞能长高一公分")//显示长文本时,后面省略号代替了
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                    .setContentIntent(pendingIntent)//设置可点击跳转
                    .setAutoCancel(true)//点击后自动取消通知(方法1)
                    .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg")))//设置声音
                    .setVibrate(new long[] {0, 1000, 1000, 1000})//使震动 数组表示 静止0秒,震动1秒 静止1秒 震动1秒
                    //声明震动的权限 <uses-permission android:name="android.permission.VIBRATE"/>
                    .setLights(Color.GREEN, 1000, 1000)//设置呼吸灯 参数:颜色  亮起时长 暗去时长
                    .setDefaults(NotificationCompat.DEFAULT_ALL)//设置默认
                    //.setStyle(new NotificationCompat.BigTextStyle().bigText("我好帅我好帅我好帅我好帅我好帅我好帅"))//显示长文本
                    .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.zachary)))//设置显示大图片
                    .setPriority(NotificationCompat.PRIORITY_MAX)//设置重要程度: PRIORITY_DEFAULT (表示默认)PRIORITY_MIN(表示最低) PRIORITY_LOW (较低)PRIORITY_HIGH (较高)PRIORITY_MAX(最高)
                    .build();
            manager.notify(1,notification);
        }
    }
渠道分类设置.jpg
通知.jpg

相关文章

  • Android8.0+ 通知适配实战代码

  • Android学习笔记之Notification

    一、需求 自定义通知栏 适配 Android 8.0 按钮点击事件 二、代码 2.1 通知栏的代码 2.2 自定义...

  • 贝聊 iPhone X 适配实战

    贝聊 iPhone X 适配实战 贝聊 iPhone X 适配实战

  • android 8.0必要适配

    android 8.0适配 配置修改 必要的适配项 通知栏 通知栏在target升级到26之后,如果不适配按照以前...

  • LayaAir屏幕适配

    LayaAir屏幕适配 官方教程链接:LayaAir实战开发11-屏幕适配 屏幕适配 随着移动端设备(手机、平板、...

  • 版本适配

    屏幕适配 代码适配 Masonry UIView+AutoLayout 可视化适配 autoLayout 系统适配...

  • iOS开发文档

    1.**关于适配** 1.代码适配:引用第三方库“SDAutoLayout”以及“Masonry”进行代码适配. ...

  • Android中Gif网络图片下载到本地并通知相册显示

    把网络中的Gif和图片下载到本地,并通知系统相册显示,同时适配了Android Q版本,上代码(kotlin): ...

  • 'CGContextAddArcToPoint'

    最近在看一篇以前的文章iOS 高效添加圆角效果实战讲解,然后跑源码,因为是早期的代码,没有适配swift3.0,运...

  • IOS适配

    分为两个部分:VFL代码适配和Masonry框架适配 1.VFL代码适配 一、VFL介绍 1、VFL全称是Visu...

网友评论

    本文标题:Android8.0+ 通知适配实战代码

    本文链接:https://www.haomeiwen.com/subject/rlobtqtx.html