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 @Importance
int 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;
}
}
}