Android8.0+ 通知適配實戰程式碼
摘要:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager)
...
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:Context2:一般不用 通常傳入03:Intent4: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