Java Code Examples for android.app.NotificationChannel#setBypassDnd()

The following examples show how to use android.app.NotificationChannel#setBypassDnd() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NotificationChannels.java    From mollyim-android with GNU General Public License v3.0 7 votes vote down vote up
@TargetApi(26)
private static @NonNull NotificationChannel copyChannel(@NonNull NotificationChannel original, @NonNull String id) {
  NotificationChannel copy = new NotificationChannel(id, original.getName(), original.getImportance());

  copy.setGroup(original.getGroup());
  copy.setSound(original.getSound(), original.getAudioAttributes());
  copy.setBypassDnd(original.canBypassDnd());
  copy.enableVibration(original.shouldVibrate());
  copy.setVibrationPattern(original.getVibrationPattern());
  copy.setLockscreenVisibility(original.getLockscreenVisibility());
  copy.setShowBadge(original.canShowBadge());
  copy.setLightColor(original.getLightColor());
  copy.enableLights(original.shouldShowLights());

  return copy;
}
 
Example 2
Source File: RankingHelper.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void createNotificationChannel(String pkg, int uid, NotificationChannel channel,
        boolean fromTargetApp, boolean hasDndAccess) {
    Preconditions.checkNotNull(pkg);
    Preconditions.checkNotNull(channel);
    Preconditions.checkNotNull(channel.getId());
    Preconditions.checkArgument(!TextUtils.isEmpty(channel.getName()));
    Record r = getOrCreateRecord(pkg, uid);
    if (r == null) {
        throw new IllegalArgumentException("Invalid package");
    }
    if (channel.getGroup() != null && !r.groups.containsKey(channel.getGroup())) {
        throw new IllegalArgumentException("NotificationChannelGroup doesn't exist");
    }
    if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channel.getId())) {
        throw new IllegalArgumentException("Reserved id");
    }
    NotificationChannel existing = r.channels.get(channel.getId());
    // Keep most of the existing settings
    if (existing != null && fromTargetApp) {
        if (existing.isDeleted()) {
            existing.setDeleted(false);

            // log a resurrected channel as if it's new again
            MetricsLogger.action(getChannelLog(channel, pkg).setType(
                    MetricsProto.MetricsEvent.TYPE_OPEN));
        }

        existing.setName(channel.getName().toString());
        existing.setDescription(channel.getDescription());
        existing.setBlockableSystem(channel.isBlockableSystem());
        if (existing.getGroup() == null) {
            existing.setGroup(channel.getGroup());
        }

        // Apps are allowed to downgrade channel importance if the user has not changed any
        // fields on this channel yet.
        if (existing.getUserLockedFields() == 0 &&
                channel.getImportance() < existing.getImportance()) {
            existing.setImportance(channel.getImportance());
        }

        // system apps and dnd access apps can bypass dnd if the user hasn't changed any
        // fields on the channel yet
        if (existing.getUserLockedFields() == 0 && hasDndAccess) {
            boolean bypassDnd = channel.canBypassDnd();
            existing.setBypassDnd(bypassDnd);

            if (bypassDnd != mAreChannelsBypassingDnd) {
                updateChannelsBypassingDnd();
            }
        }

        updateConfig();
        return;
    }
    if (channel.getImportance() < IMPORTANCE_NONE
            || channel.getImportance() > NotificationManager.IMPORTANCE_MAX) {
        throw new IllegalArgumentException("Invalid importance level");
    }

    // Reset fields that apps aren't allowed to set.
    if (fromTargetApp && !hasDndAccess) {
        channel.setBypassDnd(r.priority == Notification.PRIORITY_MAX);
    }
    if (fromTargetApp) {
        channel.setLockscreenVisibility(r.visibility);
    }
    clearLockedFields(channel);
    if (channel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) {
        channel.setLockscreenVisibility(Ranking.VISIBILITY_NO_OVERRIDE);
    }
    if (!r.showBadge) {
        channel.setShowBadge(false);
    }

    r.channels.put(channel.getId(), channel);
    if (channel.canBypassDnd() != mAreChannelsBypassingDnd) {
        updateChannelsBypassingDnd();
    }
    MetricsLogger.action(getChannelLog(channel, pkg).setType(
            MetricsProto.MetricsEvent.TYPE_OPEN));
}
 
Example 3
Source File: NotificationUtils.java    From AcgClub with MIT License 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel() {
  //第一个参数:channel_id
  //第二个参数:channel_name
  //第三个参数:设置通知重要性级别
  //注意:该级别必须要在 NotificationChannel 的构造函数中指定,总共要五个级别;
  //范围是从 NotificationManager.IMPORTANCE_NONE(0) ~ NotificationManager.IMPORTANCE_HIGH(4)
  NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME,
      NotificationManager.IMPORTANCE_DEFAULT);
  channel.canBypassDnd();//是否绕过请勿打扰模式
  channel.enableLights(true);//闪光灯
  channel.setLockscreenVisibility(VISIBILITY_SECRET);//锁屏显示通知
  channel.setLightColor(Color.RED);//闪关灯的灯光颜色
  channel.canShowBadge();//桌面launcher的消息角标
  channel.enableVibration(true);//是否允许震动
  channel.getAudioAttributes();//获取系统通知响铃声音的配置
  channel.getGroup();//获取通知取到组
  channel.setBypassDnd(true);//设置可绕过 请勿打扰模式
  channel.setVibrationPattern(new long[]{100, 100, 200});//设置震动模式
  channel.shouldShowLights();//是否会有灯光
  getManager().createNotificationChannel(channel);
}
 
Example 4
Source File: NotificationUtilites.java    From GotoSleep with GNU General Public License v3.0 6 votes vote down vote up
public static void createNotificationChannel(Context context) {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = context.getString(R.string.channel_name);
        String description = context.getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel(BEDTIME_CHANNEL_ID, name, importance);
        channel.setDescription(description);
        channel.setBypassDnd(true);
        channel.enableLights(true);
        //channel.enableVibration(true);
        channel.setLightColor(Color.BLUE);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}
 
Example 5
Source File: NotificationUtils.java    From YCUpdateApp with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel() {
    //第一个参数:channel_id
    //第二个参数:channel_name
    //第三个参数:设置通知重要性级别
    //注意:该级别必须要在 NotificationChannel 的构造函数中指定,总共要五个级别;
    //范围是从 NotificationManager.IMPORTANCE_NONE(0) ~ NotificationManager.IMPORTANCE_HIGH(4)
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME,
            NotificationManager.IMPORTANCE_LOW);
    channel.canBypassDnd();//是否绕过请勿打扰模式
    channel.enableLights(true);//是否在桌面icon右上角展示小红点
    channel.setLockscreenVisibility(VISIBILITY_SECRET);//锁屏显示通知
    channel.setLightColor(Color.RED);//闪关灯的灯光颜色
    channel.canShowBadge();//桌面launcher的消息角标
    //channel.enableVibration(false);//是否允许震动
    channel.getAudioAttributes();//获取系统通知响铃声音的配置
    channel.getGroup();//获取通知取到组
    channel.setBypassDnd(true);//设置可绕过 请勿打扰模式
    channel.setSound(null, null);
    //channel.setVibrationPattern(new long[]{100, 100, 200});//设置震动模式
    channel.shouldShowLights();//是否会有灯光
    channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
    getManager().createNotificationChannel(channel);
}
 
Example 6
Source File: NotificationUtils.java    From YCNotification with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel() {
    //第一个参数:channel_id
    //第二个参数:channel_name
    //第三个参数:设置通知重要性级别
    //注意:该级别必须要在 NotificationChannel 的构造函数中指定,总共要五个级别;
    //范围是从 NotificationManager.IMPORTANCE_NONE(0) ~ NotificationManager.IMPORTANCE_HIGH(4)
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME,
            NotificationManager.IMPORTANCE_DEFAULT);
    channel.canBypassDnd();//是否绕过请勿打扰模式
    channel.enableLights(true);//闪光灯
    channel.setLockscreenVisibility(VISIBILITY_SECRET);//锁屏显示通知
    channel.setLightColor(Color.RED);//闪关灯的灯光颜色
    channel.canShowBadge();//桌面launcher的消息角标
    channel.enableVibration(true);//是否允许震动
    channel.getAudioAttributes();//获取系统通知响铃声音的配置
    channel.getGroup();//获取通知取到组
    channel.setBypassDnd(true);//设置可绕过 请勿打扰模式
    channel.setVibrationPattern(new long[]{100, 100, 200});//设置震动模式
    channel.shouldShowLights();//是否会有灯光
    getManager().createNotificationChannel(channel);
}
 
Example 7
Source File: NotificationsManager.java    From zephyr with MIT License 6 votes vote down vote up
@Override
public void createNotificationChannels() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mLogger.log(LogLevel.INFO, LOG_TAG, "Creating notification channels...");
        // Status notification
        CharSequence name = mContext.getString(R.string.notif_channel_status_title);
        NotificationChannel channel = new NotificationChannel(ZephyrNotificationChannel.STATUS, name, NotificationManager.IMPORTANCE_LOW);
        channel.setDescription(mContext.getString(R.string.notif_channel_status_desc));
        channel.setLightColor(ContextCompat.getColor(mContext, R.color.primary));
        channel.setShowBadge(false);
        channel.setBypassDnd(false);

        NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
        mLogger.log(LogLevel.INFO, LOG_TAG, "Done creating notification channels.");
    }
}
 
Example 8
Source File: NotiHelper.java    From hipda with GNU General Public License v2.0 6 votes vote down vote up
public static void initNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Context context = HiApplication.getAppContext();

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager.getNotificationChannel(CHANNEL_ID) == null) {
            int color = ContextCompat.getColor(context, R.color.icon_blue);
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(color);
            notificationChannel.enableVibration(false);
            notificationChannel.setBypassDnd(false);
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }
}
 
Example 9
Source File: ActivitySetup.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.O)
static NotificationChannel channelFromJSON(Context context, JSONObject jchannel) throws JSONException {
    NotificationChannel channel = new NotificationChannel(
            jchannel.getString("id"),
            jchannel.getString("name"),
            jchannel.getInt("importance"));

    String group = jchannel.optString("group");
    if (!TextUtils.isEmpty(group))
        channel.setGroup(group);

    if (jchannel.has("description") && !jchannel.isNull("description"))
        channel.setDescription(jchannel.getString("description"));

    channel.setBypassDnd(jchannel.getBoolean("dnd"));
    channel.setLockscreenVisibility(jchannel.getInt("visibility"));
    channel.setShowBadge(jchannel.getBoolean("badge"));

    if (jchannel.has("sound") && !jchannel.isNull("sound")) {
        Uri uri = Uri.parse(jchannel.getString("sound"));
        Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
        if (ringtone != null)
            channel.setSound(uri, Notification.AUDIO_ATTRIBUTES_DEFAULT);
    }

    channel.enableLights(jchannel.getBoolean("light"));
    channel.enableVibration(jchannel.getBoolean("vibrate"));

    return channel;
}
 
Example 10
Source File: WeChatDecorator.java    From decorator-wechat with Apache License 2.0 5 votes vote down vote up
@RequiresApi(O) private NotificationChannel cloneChannel(final NotificationChannel channel, final String id, final int new_name) {
	final NotificationChannel clone = new NotificationChannel(id, getString(new_name), channel.getImportance());
	clone.setGroup(channel.getGroup());
	clone.setDescription(channel.getDescription());
	clone.setLockscreenVisibility(channel.getLockscreenVisibility());
	clone.setSound(Optional.ofNullable(channel.getSound()).orElse(getDefaultSound()), channel.getAudioAttributes());
	clone.setBypassDnd(channel.canBypassDnd());
	clone.setLightColor(channel.getLightColor());
	clone.setShowBadge(channel.canShowBadge());
	clone.setVibrationPattern(channel.getVibrationPattern());
	return clone;
}
 
Example 11
Source File: SteamService.java    From UpdogFarmer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create notification channel for Android O
 */
@RequiresApi(Build.VERSION_CODES.O)
private void createChannel() {
    final CharSequence name = getString(R.string.channel_name);
    final int importance = NotificationManager.IMPORTANCE_LOW;
    final NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
    channel.setShowBadge(false);
    channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    channel.enableVibration(false);
    channel.enableLights(false);
    channel.setBypassDnd(false);
    final NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.createNotificationChannel(channel);
}
 
Example 12
Source File: API26Wrapper.java    From Pedometer with Apache License 2.0 5 votes vote down vote up
public static Notification.Builder getNotificationBuilder(final Context context) {
    NotificationManager manager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel =
            new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_ID,
                    NotificationManager.IMPORTANCE_NONE);
    channel.setImportance(NotificationManager.IMPORTANCE_MIN); // ignored by Android O ...
    channel.enableLights(false);
    channel.enableVibration(false);
    channel.setBypassDnd(false);
    channel.setSound(null, null);
    manager.createNotificationChannel(channel);
    Notification.Builder builder = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID);
    return builder;
}
 
Example 13
Source File: LeanplumNotificationChannel.java    From Leanplum-Android-SDK with Apache License 2.0 4 votes vote down vote up
/**
 * Create push notification channel with provided id, name and importance of the channel.
 * You can call this method also when you need to update the name or description of a channel, at
 * this case you should use original channel id.
 *
 * @param context The application context.
 * @param channelId The id of the channel.
 * @param channelName The user-visible name of the channel.
 * @param channelImportance The importance of the channel. Use value from 0 to 5. 3 is default.
 * Read more https://developer.android.com/reference/android/app/NotificationManager.html#IMPORTANCE_DEFAULT
 * Once you create a notification channel, only the system can modify its importance.
 * @param channelDescription The user-visible description of the channel.
 * @param groupId The id of push notification channel group.
 * @param enableLights True if lights enable for this channel.
 * @param lightColor Light color for notifications posted to this channel, if the device supports
 * this feature.
 * @param enableVibration True if vibration enable for this channel.
 * @param vibrationPattern Vibration pattern for notifications posted to this channel.
 * @param lockscreenVisibility How to be shown on the lockscreen.
 * @param bypassDnd Whether should notification bypass DND.
 * @param showBadge Whether should notification show badge.
 */
private static void createNotificationChannel(Context context, String channelId, String
    channelName, int channelImportance, String channelDescription, String groupId, boolean
    enableLights, int lightColor, boolean enableVibration, long[] vibrationPattern, int
    lockscreenVisibility, boolean bypassDnd, boolean showBadge) {
  if (context == null || TextUtils.isEmpty(channelId)) {
    return;
  }
  if (BuildUtil.isNotificationChannelSupported(context)) {
    try {
      NotificationManager notificationManager =
          context.getSystemService(NotificationManager.class);
      if (notificationManager == null) {
        Log.e("Notification manager is null");
        return;
      }

      NotificationChannel notificationChannel = new NotificationChannel(channelId,
          channelName, channelImportance);
      if (!TextUtils.isEmpty(channelDescription)) {
        notificationChannel.setDescription(channelDescription);
      }
      if (enableLights) {
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(lightColor);
      }
      if (enableVibration) {
        notificationChannel.enableVibration(true);
        // Workaround for https://issuetracker.google.com/issues/63427588
        if (vibrationPattern != null && vibrationPattern.length != 0) {
          notificationChannel.setVibrationPattern(vibrationPattern);
        }
      }
      if (!TextUtils.isEmpty(groupId)) {
        notificationChannel.setGroup(groupId);
      }
      notificationChannel.setLockscreenVisibility(lockscreenVisibility);
      notificationChannel.setBypassDnd(bypassDnd);
      notificationChannel.setShowBadge(showBadge);

      notificationManager.createNotificationChannel(notificationChannel);
    } catch (Throwable t) {
      Util.handleException(t);
    }
  }
}
 
Example 14
Source File: NotificationService.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.O)
void initializeChannels() {
    final Context c = mXmppConnectionService;
    final NotificationManager notificationManager = c.getSystemService(NotificationManager.class);
    if (notificationManager == null) {
        return;
    }
    notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("status", c.getString(R.string.notification_group_status_information)));
    notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("chats", c.getString(R.string.notification_group_messages)));
    notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("calls", c.getString(R.string.notification_group_calls)));
    final NotificationChannel foregroundServiceChannel = new NotificationChannel(FOREGROUND_CHANNEL_ID,
            c.getString(R.string.foreground_service_channel_name),
            NotificationManager.IMPORTANCE_MIN);
    foregroundServiceChannel.setDescription(c.getString(R.string.foreground_service_channel_description));
    foregroundServiceChannel.setShowBadge(false);
    foregroundServiceChannel.setGroup("status");
    notificationManager.createNotificationChannel(foregroundServiceChannel);

    final NotificationChannel backupChannel = new NotificationChannel(BACKUP_CHANNEL_ID,
            c.getString(R.string.backup_channel_name),
            NotificationManager.IMPORTANCE_LOW);
    backupChannel.setShowBadge(false);
    backupChannel.setGroup("status");
    notificationManager.createNotificationChannel(backupChannel);

    final NotificationChannel videoCompressionChannel = new NotificationChannel(VIDEOCOMPRESSION_CHANNEL_ID,
            c.getString(R.string.video_compression_channel_name),
            NotificationManager.IMPORTANCE_LOW);
    videoCompressionChannel.setShowBadge(false);
    videoCompressionChannel.setGroup("status");
    notificationManager.createNotificationChannel(videoCompressionChannel);

    final NotificationChannel AppUpdateChannel = new NotificationChannel(UPDATE_CHANNEL_ID,
            c.getString(R.string.app_update_channel_name),
            NotificationManager.IMPORTANCE_LOW);
    AppUpdateChannel.setShowBadge(false);
    AppUpdateChannel.setGroup("status");
    notificationManager.createNotificationChannel(AppUpdateChannel);

    final NotificationChannel incomingCallsChannel = new NotificationChannel("incoming_calls",
            c.getString(R.string.incoming_calls_channel_name),
            NotificationManager.IMPORTANCE_HIGH);
    incomingCallsChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE), new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
            .build());
    incomingCallsChannel.setShowBadge(false);
    incomingCallsChannel.setLightColor(LED_COLOR);
    incomingCallsChannel.enableLights(true);
    incomingCallsChannel.setGroup("calls");
    incomingCallsChannel.setBypassDnd(true);
    incomingCallsChannel.enableVibration(true);
    incomingCallsChannel.setVibrationPattern(CALL_PATTERN);
    notificationManager.createNotificationChannel(incomingCallsChannel);

    final NotificationChannel ongoingCallsChannel = new NotificationChannel("ongoing_calls",
            c.getString(R.string.ongoing_calls_channel_name),
            NotificationManager.IMPORTANCE_LOW);
    ongoingCallsChannel.setShowBadge(false);
    ongoingCallsChannel.setGroup("calls");
    notificationManager.createNotificationChannel(ongoingCallsChannel);


    final NotificationChannel messagesChannel = new NotificationChannel("messages",
            c.getString(R.string.messages_channel_name),
            NotificationManager.IMPORTANCE_HIGH);
    messagesChannel.setShowBadge(true);
    messagesChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
            .build());
    messagesChannel.setLightColor(LED_COLOR);
    final int dat = 70;
    final long[] pattern = {0, 3 * dat, dat, dat};
    messagesChannel.setVibrationPattern(pattern);
    messagesChannel.enableVibration(true);
    messagesChannel.enableLights(true);
    messagesChannel.setGroup("chats");
    notificationManager.createNotificationChannel(messagesChannel);

    final NotificationChannel silentMessagesChannel = new NotificationChannel("silent_messages",
            c.getString(R.string.silent_messages_channel_name),
            NotificationManager.IMPORTANCE_LOW);
    silentMessagesChannel.setDescription(c.getString(R.string.silent_messages_channel_description));
    silentMessagesChannel.setShowBadge(true);
    silentMessagesChannel.setLightColor(LED_COLOR);
    silentMessagesChannel.enableLights(true);
    silentMessagesChannel.setGroup("chats");
    notificationManager.createNotificationChannel(silentMessagesChannel);

    final NotificationChannel quietHoursChannel = new NotificationChannel("quiet_hours",
            c.getString(R.string.title_pref_quiet_hours),
            NotificationManager.IMPORTANCE_LOW);
    quietHoursChannel.setShowBadge(true);
    quietHoursChannel.setLightColor(LED_COLOR);
    quietHoursChannel.enableLights(true);
    quietHoursChannel.setGroup("chats");
    quietHoursChannel.enableVibration(false);
    quietHoursChannel.setSound(null, null);
    notificationManager.createNotificationChannel(quietHoursChannel);
}