欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android Notification 兼容3.0到8.0

程序员文章站 2022-07-13 14:34:01
...

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)```
相关标签: Notification