由于不同的定制系统对通知的管理方式兵不一致,Notification在每个手机上的运行效果都有所差异 这里仅列举原生系统的全部用法,在真机上的实际运行效果可能有差异,请自己多加探索
String channelId = TextUtil.random();
String channelName = channelId;
int requestCode = MathUtil.randomInt();
int notificationId = MathUtil.randomInt();
String data = "hello";
//创建频道,一个频道封装了一类通知的样式
//id和name可以任意指定,importance指定通知优先级,优先级高的可以在全屏锁屏等情况下显示
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
//桌面快捷方式上显示消息角标
channel.setShowBadge(true);
//开启震动
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{200, 800, 200, 800, 200, 800});
//开启闪光灯
channel.enableLights(true);
channel.setLightColor(ColorUtil.RED);
//开启铃音提示
String soundFile = FileUtil.getAndroidExternalFile("new_msg.mp3");
channel.setSound(UriUtil.getFileUri(soundFile), Notification.AUDIO_ATTRIBUTES_DEFAULT);
//是否锁屏显示
//VISIBILITY_PUBLIC:在所有锁屏界面显示全部信息
//VISIBILITY_PRIVATE:在所有锁屏界面显示通知提示,但只在安全界面显示具体内容
//VISIBILITY_SECRET:在锁屏界面不显示任何内容
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
//创建通知构造器,必须关联一个已经存在的频道
//注意,要使用NotificationCompat.Builder,而不是Notification.Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(APP.ctx, channelId);
//设置副标题
builder.setSubText("SubTitle");
//设置内容标题
builder.setContentTitle("Notification");
//设置内容文本
builder.setContentText("this is a notification test demo");
//设置通知标题图标
builder.setSmallIcon(R.drawable.icon_favor);
//设置通知内容图标
builder.setLargeIcon(BitmapUtil.decodeBitmapFromResource(this, R.drawable.icon_setting_m01_normal));
//设置标题颜色
builder.setColor(ColorUtil.LIGHT_BLUE);
//设置桌面快捷方式的消息角标类型
builder.setBadgeIconType(Notification.BADGE_ICON_SMALL);
//设置超时,超时会自动取消
builder.setTimeoutAfter(10 * 60 * 1000);
//显示通知时间
builder.setShowWhen(true);
//点击后自动取消
builder.setAutoCancel(false);
//分组显示
builder.setGroupSummary(true);
builder.setGroup(channelId);
builder.setSound(UriUtil.getFileUri(soundFile));
//设置点击事件
//当新通知requestCode和旧通知相同时,会根据PendingIntent去更新或取消旧通知
//PendingIntent.FLAG_CANCEL_CURRENT:取消旧的PendingIntent,只有最新的通知具有点击事件
//PendingIntent.FLAG_UPDATE_CURRENT:覆盖旧的PendingIntent,所有通知的点击事件都以最新的PendingIntent为准
//PendingIntent.FLAG_ONE_SHOT:点击一次后,所有PendingIntent失效,所有通知都没有点击效果
//PendingIntent.FLAG_NO_CREATE:PendingIntent无效,没有任何点击效果
Intent intent = new Intent(APP.ctx, A.class);
intent.putExtra("data", data);
PendingIntent pi = PendingIntent.getActivity(APP.ctx, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pi);
//设置全屏通知,通知可以以悬浮式的方式,显示在应用上方
builder.setFullScreenIntent(pi, true);
//创建通知
Notification notification = builder.build();
//创建一个大视图,通知将变成折叠式的,展开后显示大视图
notification.bigContentView = new RemoteViews(APP.ctx.getPackageName(), R.layout.layout_b);
NotificationManager manager = ServiceUtil.getNotificationManager(APP.ctx);
//创建或更新通知频道,只有一部分设置允许更新,其它还会保持首次创建时的设置
manager.createNotificationChannel(channel);
//显示通知,id相同的通知会覆盖旧的通知
manager.notify(notificationId, notification);
//关于Notification使用的一些注意事项:
//NotificationChannel在创建后很多设置就不能再被修改,只能创建其它名称的通道,或者删除再重建
//需要手动去设置界面赋予APP所有的通知权限才可以,才能保证功能生效
//很多手机系统,屏蔽了铃音,闪光灯,角标等功能,无法通过通知设置,建议自己通过代码去播放铃音等
//有的手机系统,调用setFullScreenIntent不会显示悬挂式头部通知,而是直接执行通知事件,这种手机一般不要调用这个代码,直接在设置界面赋予横幅权限就可以