前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android8.0+ 通知适配实战代码

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

作者头像
Zachary46
发布2018-12-06 16:55:06
9560
发布2018-12-06 16:55:06
举报
文章被收录于专栏:Zachary46Zachary46
代码语言:javascript
复制
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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.10.29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档