【轉】Android Notification 版本適配方案

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 版本適配方案

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章