下面列出了android.app.NotificationChannel#setGroup ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Updates the name of an existing channel to match the recipient's current name. Will have no
* effect if the recipient doesn't have an existing valid channel.
*/
public static synchronized void updateContactChannelName(@NonNull Context context, @NonNull Recipient recipient) {
if (!supported() || recipient.getNotificationChannel() == null) {
return;
}
Log.i(TAG, "Updating contact channel name");
NotificationManager notificationManager = ServiceUtil.getNotificationManager(context);
if (notificationManager.getNotificationChannel(recipient.getNotificationChannel()) == null) {
Log.w(TAG, "Tried to update the name of a channel, but that channel doesn't exist.");
return;
}
NotificationChannel channel = new NotificationChannel(recipient.getNotificationChannel(),
recipient.getDisplayName(context),
NotificationManager.IMPORTANCE_HIGH);
channel.setGroup(CATEGORY_MESSAGES);
notificationManager.createNotificationChannel(channel);
}
@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;
}
/**
* Android 8以降用にNotificationChannelを生成する処理
* NotificationManager#getNotificationChannelがnullを
* 返したときのみ新規に作成する
* #createNotificationrから呼ばれる
* showNotification
* -> createNotificationBuilder
* -> (createNotificationChannel
* -> (createNotificationChannelGroup)
* -> setupNotificationChannel)
* -> createNotification
* -> startForeground -> NotificationManager#notify
* @param context
* @return
*/
@TargetApi(Build.VERSION_CODES.O)
protected void createNotificationChannel(
@NonNull final Context context) {
final NotificationManager manager
= ContextUtils.requireSystemService(context, NotificationManager.class);
if (manager.getNotificationChannel(channelId) == null) {
final NotificationChannel channel
= new NotificationChannel(channelId, channelTitle, importance);
if (!TextUtils.isEmpty(groupId)) {
createNotificationChannelGroup(context, groupId, groupName);
channel.setGroup(groupId);
}
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
manager.createNotificationChannel(setupNotificationChannel(channel));
}
}
private void createNotifyChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannelGroup(
new NotificationChannelGroup(mGroupId, mGroupName));
NotificationChannel channelMsg =
new NotificationChannel(mChannelMsgId, mChannelMsgName,
NotificationManager.IMPORTANCE_DEFAULT);
channelMsg.setDescription(mChannelMsgDes);
channelMsg.enableLights(true);
channelMsg.setLightColor(Color.BLUE);
channelMsg.enableVibration(false);
channelMsg.setVibrationPattern(new long[] { 100, 200, 300, 400 });
channelMsg.setShowBadge(true);
channelMsg.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channelMsg.setGroup(mGroupId);
mNotificationManager.createNotificationChannel(channelMsg);
NotificationChannel channelService =
new NotificationChannel(mChannelServiceId, mChannelServiceName,
NotificationManager.IMPORTANCE_LOW);
channelService.setDescription(mChannelServiceDes);
channelService.enableLights(false);
channelService.enableVibration(false);
channelService.setShowBadge(false);
channelService.setSound(null, null);
channelService.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
channelService.setGroup(mGroupId);
mNotificationManager.createNotificationChannel(channelService);
}
}
}
/**
* More verbose version of {@link #createChannelFor(Context, Recipient)}.
*/
public static synchronized @Nullable String createChannelFor(@NonNull Context context,
@NonNull String channelId,
@NonNull String displayName,
@Nullable Uri messageSound,
boolean vibrationEnabled)
{
if (!supported()) {
return null;
}
NotificationChannel channel = new NotificationChannel(channelId, displayName, NotificationManager.IMPORTANCE_HIGH);
setLedPreference(channel, TextSecurePreferences.getNotificationLedColor(context));
channel.setGroup(CATEGORY_MESSAGES);
channel.enableVibration(vibrationEnabled);
if (messageSound != null) {
channel.setSound(messageSound, new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
.build());
}
NotificationManager notificationManager = ServiceUtil.getNotificationManager(context);
notificationManager.createNotificationChannel(channel);
return channelId;
}
@TargetApi(26)
private static void onCreate(@NonNull Context context, @NonNull NotificationManager notificationManager) {
NotificationChannelGroup messagesGroup = new NotificationChannelGroup(CATEGORY_MESSAGES, context.getResources().getString(R.string.NotificationChannel_group_messages));
notificationManager.createNotificationChannelGroup(messagesGroup);
NotificationChannel messages = new NotificationChannel(getMessagesChannel(context), context.getString(R.string.NotificationChannel_messages), NotificationManager.IMPORTANCE_HIGH);
NotificationChannel calls = new NotificationChannel(CALLS, context.getString(R.string.NotificationChannel_calls), NotificationManager.IMPORTANCE_HIGH);
NotificationChannel failures = new NotificationChannel(FAILURES, context.getString(R.string.NotificationChannel_failures), NotificationManager.IMPORTANCE_HIGH);
NotificationChannel backups = new NotificationChannel(BACKUPS, context.getString(R.string.NotificationChannel_backups), NotificationManager.IMPORTANCE_LOW);
NotificationChannel lockedStatus = new NotificationChannel(LOCKED_STATUS, context.getString(R.string.NotificationChannel_locked_status), NotificationManager.IMPORTANCE_LOW);
NotificationChannel other = new NotificationChannel(OTHER, context.getString(R.string.NotificationChannel_other), NotificationManager.IMPORTANCE_LOW);
messages.setGroup(CATEGORY_MESSAGES);
messages.enableVibration(TextSecurePreferences.isNotificationVibrateEnabled(context));
messages.setSound(TextSecurePreferences.getNotificationRingtone(context), getRingtoneAudioAttributes());
setLedPreference(messages, TextSecurePreferences.getNotificationLedColor(context));
calls.setShowBadge(false);
backups.setShowBadge(false);
lockedStatus.setShowBadge(false);
other.setShowBadge(false);
notificationManager.createNotificationChannels(Arrays.asList(messages, calls, failures, backups, lockedStatus, other));
if (BuildConfig.AUTOMATIC_UPDATES) {
NotificationChannel appUpdates = new NotificationChannel(APP_UPDATES, context.getString(R.string.NotificationChannel_app_updates), NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(appUpdates);
} else {
notificationManager.deleteNotificationChannel(APP_UPDATES);
}
}
@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;
}
@RequiresApi(api = Build.VERSION_CODES.O)
void createNotificationChannel(Context context) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannelGroup group = new NotificationChannelGroup("group." + id, name);
nm.createNotificationChannelGroup(group);
NotificationChannel channel = new NotificationChannel(
getNotificationChannelId(id), name,
NotificationManager.IMPORTANCE_HIGH);
channel.setGroup(group.getId());
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
channel.enableLights(true);
nm.createNotificationChannel(channel);
}
@RequiresApi(api = Build.VERSION_CODES.O)
void createNotificationChannel(Context context) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannelGroup group = new NotificationChannelGroup("group." + accountId, accountName);
nm.createNotificationChannelGroup(group);
NotificationChannel channel = new NotificationChannel(
getNotificationChannelId(id), getDisplayName(context),
NotificationManager.IMPORTANCE_HIGH);
channel.setGroup(group.getId());
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
channel.enableLights(true);
nm.createNotificationChannel(channel);
}
@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;
}
public static void createNotificationChannel(Context ctx) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
return;
NotificationChannel channel = new NotificationChannel(IDLE_NOTIFICATION_CHANNEL,
ctx.getString(R.string.notification_channel_idle),
android.app.NotificationManager.IMPORTANCE_MIN);
channel.setGroup(NotificationManager.getSystemNotificationChannelGroup(ctx));
channel.setShowBadge(false);
android.app.NotificationManager mgr = (android.app.NotificationManager)
ctx.getSystemService(NOTIFICATION_SERVICE);
mgr.createNotificationChannel(channel);
}
public static String getNotificationChannel(Context context) {
if (!sNotificationChannelCreated && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_NAME,
context.getString(R.string.notification_channel_warning),
NotificationManager.IMPORTANCE_HIGH);
channel.setGroup(
io.mrarm.irc.NotificationManager.getSystemNotificationChannelGroup(context));
notificationManager.createNotificationChannel(channel);
sNotificationChannelCreated = true;
}
return NOTIFICATION_CHANNEL_NAME;
}
@TargetApi(26)
public static NotificationChannel getChan(Notification.Builder wip) {
final Notification temp = wip.build();
if (temp.getChannelId() == null) return null;
// create generic audio attributes
final AudioAttributes generic_audio = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.build();
// create notification channel for hashing purposes from the existing notification builder
NotificationChannel template = new NotificationChannel(
temp.getChannelId(),
getString(temp.getChannelId()),
NotificationManager.IMPORTANCE_DEFAULT);
// mirror the notification parameters in the channel
template.setGroup(temp.getChannelId());
template.setVibrationPattern(temp.vibrate);
template.setSound(temp.sound, generic_audio);
template.setLightColor(temp.ledARGB);
if ((temp.ledOnMS != 0) && (temp.ledOffMS != 0))
template.enableLights(true); // weird how this doesn't work like vibration pattern
template.setDescription(temp.getChannelId() + " " + wip.hashCode());
// get a nice string to identify the hash
final String mhash = my_text_hash(template);
// create another notification channel using the hash because id is immutable
final NotificationChannel channel = new NotificationChannel(
template.getId() + mhash,
getString(temp.getChannelId()) + mhash,
NotificationManager.IMPORTANCE_DEFAULT);
// mirror the settings from the previous channel
channel.setSound(template.getSound(), generic_audio);
if (addChannelGroup()) {
channel.setGroup(template.getGroup());
} else {
channel.setGroup(channel.getId());
}
channel.setDescription(template.getDescription());
channel.setVibrationPattern(template.getVibrationPattern());
template.setLightColor(temp.ledARGB);
if ((temp.ledOnMS != 0) && (temp.ledOffMS != 0))
template.enableLights(true); // weird how this doesn't work like vibration pattern
template.setDescription(temp.getChannelId() + " " + wip.hashCode());
// create a group to hold this channel if one doesn't exist or update text
getNotifManager().createNotificationChannelGroup(new NotificationChannelGroup(channel.getGroup(), getString(channel.getGroup())));
// create this channel if it doesn't exist or update text
getNotifManager().createNotificationChannel(channel);
return channel;
}
@RequiresApi(api = Build.VERSION_CODES.O)
public static void createNotificationChannels(Context context){
NotificationManager notificationManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
List<NotificationChannelGroup> notificationChannelGroups=new ArrayList<>();
notificationChannelGroups.add(new NotificationChannelGroup("hify","Hify"));
notificationChannelGroups.add(new NotificationChannelGroup("other","Other"));
notificationManager.createNotificationChannelGroups(notificationChannelGroups);
NotificationChannel flash_message_channel=new NotificationChannel("flash_message","Flash Messages",NotificationManager.IMPORTANCE_HIGH);
flash_message_channel.enableLights(true);
flash_message_channel.enableVibration(true);
flash_message_channel.setGroup("hify");
flash_message_channel.setSound(Uri.parse("android.resource://"+context.getPackageName()+"/"+ R.raw.hify_sound), null);
NotificationChannel comments_channel=new NotificationChannel("comments_channel","Comments",NotificationManager.IMPORTANCE_HIGH);
comments_channel.enableLights(true);
comments_channel.enableVibration(true);
comments_channel.setGroup("hify");
comments_channel.setSound(Uri.parse("android.resource://"+context.getPackageName()+"/"+ R.raw.hify_sound), null);
NotificationChannel like_channel=new NotificationChannel("like_channel","Likes",NotificationManager.IMPORTANCE_HIGH);
like_channel.enableLights(true);
like_channel.enableVibration(true);
like_channel.setGroup("hify");
like_channel.setSound(Uri.parse("android.resource://"+context.getPackageName()+"/"+ R.raw.hify_sound), null);
NotificationChannel forum_channel=new NotificationChannel("forum_channel","Forum",NotificationManager.IMPORTANCE_HIGH);
forum_channel.enableLights(true);
forum_channel.enableVibration(true);
forum_channel.setGroup("hify");
forum_channel.setSound(Uri.parse("android.resource://"+context.getPackageName()+"/"+ R.raw.hify_sound), null);
NotificationChannel sending_channel=new NotificationChannel("sending_channel","Sending Media",NotificationManager.IMPORTANCE_LOW);
sending_channel.enableLights(true);
sending_channel.enableVibration(true);
sending_channel.setGroup("other");
NotificationChannel other_channel=new NotificationChannel("other_channel","Other Notifications",NotificationManager.IMPORTANCE_LOW);
other_channel.enableLights(true);
other_channel.enableVibration(true);
other_channel.setGroup("other");
NotificationChannel hify_other_channel=new NotificationChannel("hify_other_channel","Other Notifications",NotificationManager.IMPORTANCE_LOW);
hify_other_channel.enableLights(true);
hify_other_channel.enableVibration(true);
hify_other_channel.setGroup("hify");
hify_other_channel.setSound(Uri.parse("android.resource://"+context.getPackageName()+"/"+ R.raw.hify_sound), null);
List<NotificationChannel> notificationChannels=new ArrayList<>();
notificationChannels.add(flash_message_channel);
notificationChannels.add(like_channel);
notificationChannels.add(comments_channel);
notificationChannels.add(forum_channel);
notificationChannels.add(sending_channel);
notificationChannels.add(other_channel);
notificationChannels.add(hify_other_channel);
notificationManager.createNotificationChannels(notificationChannels);
}
/**
* 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);
}
}
}
public static void createChannel(Context context, NotificationRule rule) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || rule.settings.noNotification)
return;
NotificationRuleManager.loadUserRuleSettings(context);
String id = rule.settings.notificationChannelId;
if (id == null) {
id = UUID.randomUUID().toString();
rule.settings.notificationChannelId = id;
NotificationRuleManager.saveUserRuleSettings(context);
}
String name = rule.getName();
if (name == null)
name = context.getString(rule.getNameId());
NotificationChannel channel = new NotificationChannel(id, name,
android.app.NotificationManager.IMPORTANCE_HIGH);
if (rule.getNameId() != -1)
channel.setGroup(NotificationManager.getDefaultNotificationChannelGroup(context));
else
channel.setGroup(NotificationManager.getUserNotificationChannelGroup(context));
if (rule.settings.soundEnabled) {
if (rule.settings.soundUri != null)
channel.setSound(Uri.parse(rule.settings.soundUri), new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
.build());
} else {
channel.setSound(null, null);
}
if (rule.settings.vibrationEnabled) {
channel.enableVibration(true);
if (rule.settings.vibrationDuration != 0)
channel.setVibrationPattern(new long[]{0, rule.settings.vibrationDuration});
}
if (rule.settings.lightEnabled) {
channel.enableLights(true);
if (rule.settings.light != 0)
channel.setLightColor(rule.settings.light);
}
android.app.NotificationManager mgr = (android.app.NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
mgr.createNotificationChannel(channel);
}
@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);
}
@TargetApi(26)
public static NotificationChannel getChan(NotificationCompat.Builder wip) {
final Notification temp = wip.build();
if (temp.getChannelId() == null) return null;
// create generic audio attributes
final AudioAttributes generic_audio = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.build();
// create notification channel for hashing purposes from the existing notification builder
NotificationChannel template = new NotificationChannel(
temp.getChannelId(),
getString(temp.getChannelId()),
NotificationManager.IMPORTANCE_DEFAULT);
// mirror the notification parameters in the channel
template.setGroup(temp.getChannelId());
template.setVibrationPattern(wip.mNotification.vibrate);
template.setSound(wip.mNotification.sound, generic_audio);
template.setLightColor(wip.mNotification.ledARGB);
if ((wip.mNotification.ledOnMS != 0) && (wip.mNotification.ledOffMS != 0))
template.enableLights(true); // weird how this doesn't work like vibration pattern
template.setDescription(temp.getChannelId() + " " + wip.hashCode());
// get a nice string to identify the hash
final String mhash = my_text_hash(template);
// create another notification channel using the hash because id is immutable
final NotificationChannel channel = new NotificationChannel(
template.getId() + mhash,
getString(temp.getChannelId()) + mhash,
NotificationManager.IMPORTANCE_DEFAULT);
// mirror the settings from the previous channel
channel.setSound(template.getSound(), generic_audio);
if (addChannelGroup()) {
channel.setGroup(template.getGroup());
} else {
channel.setGroup(channel.getId());
}
channel.setDescription(template.getDescription());
channel.setVibrationPattern(template.getVibrationPattern());
template.setLightColor(wip.mNotification.ledARGB);
if ((wip.mNotification.ledOnMS != 0) && (wip.mNotification.ledOffMS != 0))
template.enableLights(true); // weird how this doesn't work like vibration pattern
template.setDescription(temp.getChannelId() + " " + wip.hashCode());
// create a group to hold this channel if one doesn't exist or update text
getNotifManager().createNotificationChannelGroup(new NotificationChannelGroup(channel.getGroup(), getString(channel.getGroup())));
// create this channel if it doesn't exist or update text
getNotifManager().createNotificationChannel(channel);
return channel;
}
private String getNotificationChannel(NotificationManagerCompat notificationManager, DcChat dcChat) {
int chatId = dcChat.getId();
String channelId = CH_MSG_PREFIX;
if (notificationChannelsSupported()) {
try {
// get all values we'll use as settings for the NotificationChannel
String ledColor = Prefs.getNotificationLedColor(context);
boolean defaultVibrate = effectiveVibrate(chatId);
@Nullable Uri ringtone = effectiveSound(chatId);
boolean isIndependent = requiresIndependentChannel(chatId);
// get channel id from these settings
channelId = computeChannelId(ledColor, defaultVibrate, ringtone, isIndependent? chatId : 0);
// user-visible name of the channel -
// we just use the name of the chat or "Default"
// (the name is shown in the context of the group "Chats" - that should be enough context)
String name = context.getString(R.string.def);
if (isIndependent) {
name = dcChat.getName();
}
// check if there is already a channel with the given name
List<NotificationChannel> channels = notificationManager.getNotificationChannels();
boolean channelExists = false;
for (int i = 0; i < channels.size(); i++) {
String currChannelId = channels.get(i).getId();
if (currChannelId.startsWith(CH_MSG_PREFIX)) {
// this is one of the message channels handled here ...
if (currChannelId.equals(channelId)) {
// ... this is the actually required channel, fine :)
// update the name to reflect localize changes and chat renames
channelExists = true;
channels.get(i).setName(name);
} else {
// ... another message channel, delete if it is not in use.
int currChatId = parseNotificationChannelChatId(currChannelId);
if (!currChannelId.equals(computeChannelId(ledColor, effectiveVibrate(currChatId), effectiveSound(currChatId), currChatId))) {
notificationManager.deleteNotificationChannel(currChannelId);
}
}
}
}
// create a channel with the given settings;
// we cannot change the settings, however, this is handled by using different values for chId
if(!channelExists) {
NotificationChannel channel = new NotificationChannel(channelId, name, NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Informs about new messages.");
channel.setGroup(getNotificationChannelGroup(notificationManager));
channel.enableVibration(defaultVibrate);
channel.setShowBadge(true);
if (!ledColor.equals("none")) {
channel.enableLights(true);
channel.setLightColor(getLedArgb(ledColor));
} else {
channel.enableLights(false);
}
if (ringtone != null && !TextUtils.isEmpty(ringtone.toString())) {
channel.setSound(ringtone,
new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
.build());
}
notificationManager.createNotificationChannel(channel);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
return channelId;
}
@TargetApi(26)
public static NotificationChannel getChan(NotificationCompat.Builder wip) {
final Notification temp = wip.build();
if (temp.getChannelId() == null) return null;
// create generic audio attributes
final AudioAttributes generic_audio = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.build();
// create notification channel for hashing purposes from the existing notification builder
NotificationChannel template = new NotificationChannel(
temp.getChannelId(),
getString(temp.getChannelId()),
NotificationManager.IMPORTANCE_DEFAULT);
// mirror the notification parameters in the channel
template.setGroup(temp.getChannelId());
template.setVibrationPattern(wip.mNotification.vibrate);
template.setSound(wip.mNotification.sound, generic_audio);
template.setLightColor(wip.mNotification.ledARGB);
if ((wip.mNotification.ledOnMS != 0) && (wip.mNotification.ledOffMS != 0))
template.enableLights(true); // weird how this doesn't work like vibration pattern
template.setDescription(temp.getChannelId() + " " + wip.hashCode());
// get a nice string to identify the hash
final String mhash = my_text_hash(template);
// create another notification channel using the hash because id is immutable
final NotificationChannel channel = new NotificationChannel(
template.getId() + mhash,
getString(temp.getChannelId()) + mhash,
NotificationManager.IMPORTANCE_DEFAULT);
// mirror the settings from the previous channel
channel.setSound(template.getSound(), generic_audio);
if (addChannelGroup()) {
channel.setGroup(template.getGroup());
} else {
channel.setGroup(channel.getId());
}
channel.setDescription(template.getDescription());
channel.setVibrationPattern(template.getVibrationPattern());
template.setLightColor(wip.mNotification.ledARGB);
if ((wip.mNotification.ledOnMS != 0) && (wip.mNotification.ledOffMS != 0))
template.enableLights(true); // weird how this doesn't work like vibration pattern
template.setDescription(temp.getChannelId() + " " + wip.hashCode());
// create a group to hold this channel if one doesn't exist or update text
getNotifManager().createNotificationChannelGroup(new NotificationChannelGroup(channel.getGroup(), getString(channel.getGroup())));
// create this channel if it doesn't exist or update text
getNotifManager().createNotificationChannel(channel);
return channel;
}