Notification Failed to post notification on channel “null”

Android系统在 8.0 以后增加了通知通道,要正确的在 8.0 系统上使用通知,需要进行版本判定,
然后进行适配,创建出 Builder以后,其他操作不变。

 manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
...
Notification.Builder builder;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    String channelId = "notification_simple";
    NotificationChannel channel = new NotificationChannel(channelId, "simple", NotificationManager.IMPORTANCE_DEFAULT);
    manager.createNotificationChannel(channel);
    builder = new Notification.Builder(this, channelId);
} else {
    builder = new Notification.Builder(this);
}

Intent intent = new Intent(this, SimpleCardViewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

// General settings
builder.setContentTitle("simple notification");
builder.setContentText("notification text");
builder.setContentIntent(pendingIntent);
builder.setTicker("");//首次收到的时候,在状态栏中,图标的右侧显示的文字
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setAutoCancel(true);

.setDefaults(Notification.DEFAULT_ALL);//打开呼吸灯,声音,震动,触发系统默认行为
/*Notification.DEFAULT_VIBRATE    //添加默认震动提醒  需要VIBRATE permission
Notification.DEFAULT_SOUND    //添加默认声音提醒
Notification.DEFAULT_LIGHTS//添加默认三色灯提醒
Notification.DEFAULT_ALL//添加默认以上3种全部提醒*/
//.setLights(Color.YELLOW, 300, 0)//单独设置呼吸灯,一般三种颜色:红,绿,蓝,经测试,小米支持黄色
//.setSound(url)//单独设置声音
//.setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })//单独设置震动


//比较手机sdk版本与Android 5.0 Lollipop的sdk
if(android.os.Build.VERSION.SDK_INT>= android.os.Build.VERSION_CODES.LOLLIPOP) {

  /*android5.0加入了一种新的模式Notification的显示等级,共有三种:
  VISIBILITY_PUBLIC只有在没有锁屏时会显示通知
  VISIBILITY_PRIVATE任何情况都会显示通知
  VISIBILITY_SECRET在安全锁和没有锁屏的情况下显示通知*/
builder
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setPriority(Notification.PRIORITY_DEFAULT)//设置该通知优先级
.setCategory(Notification.CATEGORY_MESSAGE)//设置通知类别
//.setColor(context.getResources().getColor(R.color.small_icon_bg_color))//设置smallIcon的背景色
.setFullScreenIntent(contentIntent, true)//将Notification变为悬挂式Notification,使用这种模式的时候,activity 必须处于全屏状态,否则无效
}

// Show notification
manager.notify(0, builder.build());
如果使用呼吸灯或者是震动,需要加上权限

<!-- 闪光灯权限 -->
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<!-- 振动器权限 -->
<uses-permission android:name="android.permission.VIBRATE"/>

通知的几种常见形式

Notification 折叠模式默认是展开的
如果在构建 builder 的时候,指定的自定义展开View 高度很小的话,就默认是展开的,比如说20dp,
如果高度大于默认的高度的话,比如说100dp,那么默认就是折叠的。

RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notification_flod);
Notification notification = builder.build();
notification.bigContentView = remoteViews;
manager.notify(0, notification);

长文字类型通知

android.support.v4.app.NotificationCompat.BigTextStyle style = new android.support.v4.app.NotificationCompat.BigTextStyle();
style.bigText("android.support.v4.app.NotificationCompat.BigTextStyle style = new android.support.v4.app.NotificationCompat.BigTextStyle();");
style.setBigContentTitle("点击后的标题");
builder.setStyle(style);

多行文字类型通知

android.support.v4.app.NotificationCompat.InboxStyle style = new android.support.v4.app.NotificationCompat.InboxStyle();
style.setBigContentTitle("MutilLineTitle")
        .addLine("第一行,第一行,第一行,第一行,第一行,第一行,第一行")
        .addLine("第二行")
        .addLine("第三行")
        .addLine("第四行")
        .addLine("第五行")
builder.setStyle(style);

大图类型通知

android.support.v4.app.NotificationCompat.BigPictureStyle style = new android.support.v4.app.NotificationCompat.BigPictureStyle();
style.setBigContentTitle("BigContentTitle");
style.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.game));
builder.setStyle(style);

进度条类型通知(超级简版)

builder.setContentTitle("下载中...99%");
builder.setProgress(100, 99, false);
//禁止滑动删除
builder.setOngoing(true);
//取消右上角的时间显示
builder.setShowWhen(false);

猜你喜欢

转载自blog.csdn.net/xiao6gui/article/details/80895527