java android8.0以后,通知需要单独适配。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java android8.0以后,通知需要单独适配。相关的知识,希望对你有一定的参考价值。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      String channelId = "my_channel";
      String channelName = "My Channel";
      NotificationChannel chan = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
      chan.setLightColor(Color.BLUE);
      chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
      NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      service.createNotificationChannel(chan);
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
      Notification notification = notificationBuilder.setOngoing(true)
//              .setSmallIcon(R.drawable.ic_launcher_foreground)
              .setCategory(Notification.CATEGORY_SERVICE)
              .setPriority(Notification.PRIORITY_MIN)
              .build();
      startForeground(1234, notification);//该方法已创建通知管理器,设置为前台优先级后,点击通知不再自动取消
    } else {
      Notification.Builder mBuilder = new Notification.Builder(this)
              .setSmallIcon(R.drawable.ic_launcher)
              .setContentTitle("远离手机")
              .setContentText("定时锁机运行中")
//            .setContentIntent(contentIntent)
              .setPriority(Notification.PRIORITY_MIN);
      Notification notification = mBuilder.build();
      startForeground(1234, notification);//该方法已创建通知管理器,设置为前台优先级后,点击通知不再自动取消
    }

以上是关于java android8.0以后,通知需要单独适配。的主要内容,如果未能解决你的问题,请参考以下文章

Android8.0通知

Meteor raix:android 8.0.0 的推送通知 channelId

Android8.0新特性总结

Android开发学习——使用通知

Android 实现通知栏和进度条效果(适用于Android8.0以上)

8.0通知栏新增通知渠道