8.0后通知增加channel机制,需要对不同版本进行适配
实现了一个适配Androi8.0的通知类
public class MyNotification {/*** 发送通知方法*/public static void sendNotification(Context context, int notifNum) {// 通知功能NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);// 新版适配if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {NotificationChannel nChannel = new NotificationChannel("channel001", "通道001", NotificationManager.IMPORTANCE_HIGH);nManager.createNotificationChannel(nChannel);Notification notification = getNotificationWithChannel("通知", "这是当前通知计数" + notifNum, "channel001", context);nManager.notify(0x001, notification);}// 旧版else {Notification notification = getNotification("通知", "这是当前通知计数" + notifNum, context);nManager.notify(0x001, notification);}System.out.println("notification异步任务启动输出测试");}/*** 新版适配*/@TargetApi(Build.VERSION_CODES.O)public static Notification getNotificationWithChannel(String Title, String Content, String ChannelId, Context context) {Notification.Builder builder = new Notification.Builder(context, ChannelId);return builder.setContentTitle(Title).setContentText(Content).setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.img0).build();}/*** 旧版方法*/public static Notification getNotification(String Title, String Content, Context context) {NotificationCompat.Builder builder = new NotificationCompat.Builder(context);return builder.setContentTitle(Title).setContentText(Content).setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.img0).build();}}
