Android Notification 版本适配方案

自己项目中走过的坑 请大家不要跳坑
因为当时看文章中介绍只适配了8.0 现在运行到8.0以下的手机出现一个错误
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);

最后整合的代码

public void showNotification(){
 
        final int NOTIFICATION_ID = 12234;
 
        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        //准备intent
        Intent intent = new Intent();
        String action = "com.tamic.myapp.action";
        intent.setAction(action);
 
        //notification
        Notification notification = null;
        String contentText;
        // 构建 PendingIntent
        PendingIntent pi = PendingIntent.getActivity(mContext, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        //版本兼容
 
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            notification = new Notification();
            notification.icon = android.R.drawable.stat_sys_download_done;
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.setLatestEventInfo(mContext, aInfo.mFilename, contentText, pi);
 
        } else if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O && Build.VERSION.SDK_INT >= LOLLIPOP_MR1) {
            notification = new NotificationCompat.Builder(mContext)
                    .setContentTitle("Title")
                    .setContentText(contentText)
                    .setSmallIcon(android.R.drawable.stat_sys_download_done)
                    .setContentIntent(pi).build();
 
        } else if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN &&
                Build.VERSION.SDK_INT <= LOLLIPOP_MR1) {
            notification = new Notification.Builder(mContext)
                    .setAutoCancel(false)
                    .setContentIntent(pi)
                    .setSmallIcon(android.R.drawable.stat_sys_download_done)
                    .setWhen(System.currentTimeMillis())
                    .build();
        } else 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);
 
            notification = new NotificationCompat.Builder(ctx, CHANNEL_ID)
                    .setSmallIcon(android.R.drawable.stat_sys_download_done)
                    .setContentTitle("Title").build();
            
        }
 
        notificationManager.notify(NOTIFICATION_ID, notification);
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,383评论 2 59
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,900评论 25 708
  • 原文出处: http://www.androidchina.net/6174.html Notification在...
    木木00阅读 12,560评论 3 32
  • 最近公司有一个通知的功能,于是想起之前看过的Notification,当时看的时候看到网上有很多的讲解和代码,就觉...
    馒头炖土豆阅读 1,727评论 3 1
  • 真想 花上几十元 做几个轮子 把老家 三百平米的房子 轻轻放上去 招呼几个房奴 喊几声号子 推到上海去 在世博园 ...
    层林尽染林溪边阅读 178评论 0 2

友情链接更多精彩内容