Android Notification 兼容3.0到8.0
Build.VERSION.SDK_INT<11
http://www.cnblogs.com/hasse/p/5164417.html
//Android 4.1之前 创建一个Notification
Notification notification = new Notification();
//设置事件信息
notification.setLatestEventInfo(context,title,content, pendingIntent);
Build.VERSION.SDK_INT >= 11 && Build.VERSION.SDK_INT < 22
//使用Notification.Builder创建一个Notification
Notification.Builder builder = new Notification.Builder(this);
Notification notification= builder.getNotification();// 调用getNotification()来生成Notification
//其实也可以这么写
Notification notification = builder.build();//将builder对象转换为普通的notification
//实际上builder.getNotification();里面的实现就是builder.build()
Build.VERSION.SDK_INT >=22 && Build.VERSION.SDK_INT < 26
//使用NotificationCompat.Builder创建一个Notification
Notification notification = new NotificationCompat.Builder(mContext).build();
https://www.jianshu.com/p/6187fe8a0704
Build.VERSION.SDK_INT>=26
Android系统在 8.0 以后增加了通知通道,要正确的在 8.0 系统上使用通知,
需要进行版本判定, 然后进行适配,创建出 Builder以后,其他操作不变。
如果不对其进行处理的话会出现:
Failed to post notification on channel "null"的错误提示。
https://blog.csdn.net/qq_39622065/article/details/83033167
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
String channelId = "notification_simple";
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(channelId, "simple", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
参考文章
8.0 通知后系统界面崩溃,无限闪屏
https://www.jianshu.com/p/06e04ac55db3
Notification、Notification.Builder 和NotificationCompat.Builder的区别 https://blog.csdn.net/weixin_43174412/article/details/82629985
不同版本Notification的显示问题
https://blog.csdn.net/qq_30393319/article/details/52121331
其他问题
//我的代码
Notification.Builder builder = new Notification.Builder(context);
Bitmap largeIcon largeIcon = BitmapUtil.getLoacalBitmapFromUrl(imageUrl);
//imageUrl是网络图片地址、先从网上下载图片到文件夹中、然后把它转为Bitmap
builder.setLargeIcon(largeIcon);
// 6.0 报错 等待解决
Caused by: java.lang.AbstractMethodError: abstract method "android.graphics.drawable.Drawable android.content.pm.PackageManager.getUserBadgeForDensity(android.os.UserHandle, int)"
at android.app.Notification$Builder.getProfileBadgeDrawable(Notification.java:2890)
at android.app.Notification$Builder.hasThreeLines(Notification.java:3105)
at android.app.Notification$Builder.build(Notification.java:3660)```
上一篇: 使用POI 读和写excel的工具类
下一篇: Apache POI 简单使用教程
推荐阅读
-
Android Studio 升级到3.0后输入法中文状态下无法选词的终极解决方案
-
解决Android Studio新版本(3.0以上)与butterknife不兼容的问题
-
升级到Android Studio3.0遇到的问题及解决方案
-
Android Studio 升级到3.0 提示 java.lang.NoClassDefFoundError的解决方法
-
Android自定义Notification兼容笔记
-
Android Notification 兼容3.0到8.0
-
Android8.0、9.0、10.0的新特性兼容适配代码修改
-
Android兼容性优化-Android 8.0设置Activity透明主题崩溃
-
Android O 8.0 Notification 源码分析(二)
-
Android Notification(Android 8.0)