Notification通知辅助类

Notification通知辅助类

public class NotificationHelper {public static NotificationManager getNotificationManager(Context context) {return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);}public static Notification getNotification(Context context, NotificationConfig config) {NotificationManager manager = getNotificationManager(context);NotificationCompat.Builder builder;CheckValueUtils.checkNotNull(config);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {        //Android 8.0适配NotificationChannel channel = new NotificationChannel(config.getChannelId(),config.getChannelName(), config.importance);config.config(channel);manager.createNotificationChannel(channel);builder = new NotificationCompat.Builder(context, config.getChannelId());} else {builder = new NotificationCompat.Builder(context);}return config.setBuild(builder).build();}@RequiresApi(api = Build.VERSION_CODES.N)@IntDef(value = {IMPORTANCE_UNSPECIFIED, IMPORTANCE_NONE, IMPORTANCE_MIN, IMPORTANCE_LOW, IMPORTANCE_DEFAULT, IMPORTANCE_HIGH})@Retention(RetentionPolicy.SOURCE)@interface Importance {}public static class NotificationConfig {private String channelId;private String channelName;private @Importanceint importance;public NotificationConfig() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {this.channelId = "1";this.channelName = AppUtils.getAppName();this.importance = IMPORTANCE_HIGH;}}@TargetApi(Build.VERSION_CODES.O)public NotificationConfig(String channelId, String channelName, @Importance int importance) {this.channelId = channelId;this.channelName = channelName;this.importance = importance;}@TargetApi(Build.VERSION_CODES.O)public NotificationChannel config(NotificationChannel channel) {// 开启指示灯channel.enableLights(true);// 设置指示灯颜色channel.setLightColor(Color.GREEN);// 设置是否应在锁定屏幕上显示此频道的通知channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);// 是否绕过请勿打扰模式channel.canBypassDnd();//  设置绕过免打扰模式channel.setBypassDnd(true);// 桌面Launcher的消息角标channel.canShowBadge();// 设置显示桌面Launcher的消息角标channel.setShowBadge(true);// 开启震动channel.enableVibration(true);// 设置震动频率 静止1秒,震动1秒,静止1秒,震动1秒channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000});return channel;}public NotificationCompat.Builder setBuild(NotificationCompat.Builder builder) {builder.setWhen(System.currentTimeMillis())                //通知创建的时间.setSmallIcon(BaseApplication.getInstance().getApplicationInfo().icon);    //通知显示的小图标,只能用alpha图层的图片进行设置return builder;}@TargetApi(Build.VERSION_CODES.O)public String getChannelId() {return channelId;}@TargetApi(Build.VERSION_CODES.O)public String getChannelName() {return channelName;}@TargetApi(Build.VERSION_CODES.O)public int getImportance() {return importance;}@TargetApi(Build.VERSION_CODES.O)public void setChannelId(String channelId) {this.channelId = channelId;}@TargetApi(Build.VERSION_CODES.O)public void setChannelName(String channelName) {this.channelName = channelName;}@TargetApi(Build.VERSION_CODES.O)public void setImportance(int importance) {this.importance = importance;}}
}