【转】Android Notification 版本适配方案

    技术2022-07-11  133

    Notification 介绍见:https://developer.android.com/reference/android/app/Notification.html

    Android api 一直对通知栏进行升级! 包括7.0继续改善快捷通知栏,接下来介绍下通知栏不同版本的兼容适配.

    **Android  JELLY_BEAN(16) 通知可以直接new Notification()**

         Notification notification = new Notification();      notification.icon = android.R.drawable.stat_sys_download_done;      notification.flags |= Notification.FLAG_AUTO_CANCEL;     // 设置点击事件的PendingIntent      notification.setLatestEventInfo(mContext, aInfo.mFilename, contentText, pendingIntent);

    **Android .LOLLIPOP_MR1(22) 通知可以通过Notification.Builder()**

       Notification notification = new Notification.Builder(mContext)      .setAutoCancel(false)      .setContentIntent(pi)// 设置pendingIntent      .setSmallIcon(android.R.drawable.stat_sys_download_done)      .setWhen(System.currentTimeMillis())      .build();

    **Android .LOLLIPOP_MR1(22)以上 也就从6.0开始  只能通过new NotificationCompat.Builder(mContext)**

       Notification notification = new NotificationCompat.Builder(mContext)      .setContentTitle(aInfo.mFilename)      .setContentText(contentText)      .setSmallIcon(android.R.drawable.stat_sys_download_done)      .setContentIntent(pi)// 设置pendingIntent      .build();

    **Android .O以上 也就从8.0开始  需要制定chanel属性**

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { String CHANNEL_ID = "my_channel_01"; CharSequence name = "my_channel"; String Description = "This is my channel"; int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance); mChannel.setDescription(Description); mChannel.enableLights(true); mChannel.setLightColor(Color.RED); mChannel.enableVibration(true); mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); mChannel.setShowBadge(false); notificationManager.createNotificationChannel(mChannel); } NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(title) .setContentText(message);

    原文链接 Android Notification 版本适配方案

    Processed: 0.010, SQL: 9