```java
## 安卓8.0以上的通知栏适配
项目中遇到的问题,此贴留做记录。
Intent intent = new Intent(BackService.this, LoadingActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(BackService.this, 0, intent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification notification;
Notification.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = new Notification.Builder(BackService.this, "5996773");
} else {
builder = new Notification.Builder(BackService.this);
}
//设置标题
builder.setContentTitle("我是标题") //显示通知的标题
.setContentText("我是内容")//显示消息通知的内容
.setWhen(System.currentTimeMillis())//显示通知的具体时间
.setSmallIcon(R.mipmap.ic_launcher)//这里设置显示的是手机顶部系统通知的应用图标
.setAutoCancel(false)//点击通知后通知消失
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//这里设置显示的是下拉通知栏后显示的系统图标
.setContentIntent(pendingIntent)
//.setAutoCancel(true)//可以在此使用此方法,点击通知后,通知内容自动取消,也可以在NotificationActivity.java中设置方法取消显示通知内容
.setVibrate(new long[]{0, 1000, 1000, 1000});//设置发出通知后震动一秒,停止一秒后再震动一秒,需要在manifest.xml中设置权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("5996773", "安卓10a", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);//是否在桌面icon右上角展示小红点
channel.setLightColor(Color.GREEN);//小红点颜色
channel.setShowBadge(false); //是否在久按桌面图标时显示此渠道的通知
notificationManager.createNotificationChannel(channel);
}
notification = builder.build();
notificationManager.notify(1, notification);
} else {
Notification notification = new Notification.Builder(BackService.this)
.setContentTitle("我是标题") //显示通知的标题
.setContentText("我是内容")//显示消息通知的内容
.setWhen(System.currentTimeMillis())//显示通知的具体时间
.setSmallIcon(R.mipmap.ic_launcher)//这里设置显示的是手机顶部系统通知的应用图标
.setAutoCancel(false)//点击通知后通知消失
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//这里设置显示的是下拉通知栏后显示的系统图标
.setContentIntent(pendingIntent)
//.setAutoCancel(true)//可以在此使用此方法,点击通知后,通知内容自动取消,也可以在NotificationActivity.java中设置方法取消显示通知内容
.setVibrate(new long[]{0, 1000, 1000, 1000})//设置发出通知后震动一秒,停止一秒后再震动一秒,需要在manifest.xml中设置权限
.build();
notificationManager.notify(2, notification);
}
安卓8.0以上的通知栏适配
最新推荐文章于 2024-04-27 17:30:34 发布