【转】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 版本适配方案

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值