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+ 通知适配实战代码

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