下面列出了android.app.NotificationManager#createNotificationChannelGroup ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Create push notification channel group.
*
* @param context The application context.
* @param groupId The id of the group.
* @param groupName The user-visible name of the group.
*/
private static void createNotificationGroup(Context context, String groupId, String groupName) {
if (context == null || TextUtils.isEmpty(groupId)) {
return;
}
if (BuildUtil.isNotificationChannelSupported(context)) {
try {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null) {
Log.e("Notification manager is null");
return;
}
notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(groupId,
groupName));
} catch (Throwable t) {
Util.handleException(t);
}
}
}
@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)
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(api = Build.VERSION_CODES.O)
private void notificationGroupInit(int group_id, int group_name) {
// 通知渠道组的id.
String group = getString(group_id);
// 用户可见的通知渠道组名称.
CharSequence name = getString(group_name);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannelGroup(new NotificationChannelGroup(group, name));
}
public static NotificationChannel registerChannelIfNeeded(Context context, String packageName) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return null;
}
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = getChannelIdByPkg(packageName);
NotificationChannel notificationChannel = manager.getNotificationChannel(channelId);
if (notificationChannel != null) {
if (ID_GROUP_APPLICATIONS.equals(notificationChannel.getGroup()) || TextUtils.isEmpty(notificationChannel.getGroup())) {
manager.deleteNotificationChannel(channelId);
notificationChannel = null;
}
}
if (notificationChannel == null) {
CharSequence name = ApplicationNameCache.getInstance().getAppName(context, packageName);
if (name == null) {
return null;
}
NotificationChannelGroup notificationChannelGroup = createGroupWithPackage(packageName, name);
manager.createNotificationChannelGroup(notificationChannelGroup);
notificationChannel = createChannelWithPackage(packageName, name);
notificationChannel.setGroup(notificationChannelGroup.getId());
manager.createNotificationChannel(notificationChannel);
}
return notificationChannel;
}
private void createNotificationChannels() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sync
NotificationChannel service = new NotificationChannel(
"service", getString(R.string.channel_service),
NotificationManager.IMPORTANCE_MIN);
service.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
service.setShowBadge(false);
service.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
nm.createNotificationChannel(service);
// Send
NotificationChannel send = new NotificationChannel(
"send", getString(R.string.channel_send),
NotificationManager.IMPORTANCE_DEFAULT);
send.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
send.setShowBadge(false);
send.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
nm.createNotificationChannel(send);
// Notify
NotificationChannel notification = new NotificationChannel(
"notification", getString(R.string.channel_notification),
NotificationManager.IMPORTANCE_HIGH);
notification.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notification.enableLights(true);
nm.createNotificationChannel(notification);
// Update
if (!Helper.isPlayStoreInstall()) {
NotificationChannel update = new NotificationChannel(
"update", getString(R.string.channel_update),
NotificationManager.IMPORTANCE_HIGH);
update.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
update.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
nm.createNotificationChannel(update);
}
// Warnings
NotificationChannel warning = new NotificationChannel(
"warning", getString(R.string.channel_warning),
NotificationManager.IMPORTANCE_HIGH);
warning.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
nm.createNotificationChannel(warning);
// Errors
NotificationChannel error = new NotificationChannel(
"error",
getString(R.string.channel_error),
NotificationManager.IMPORTANCE_HIGH);
error.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
nm.createNotificationChannel(error);
// Server alerts
NotificationChannel alerts = new NotificationChannel(
"alerts",
getString(R.string.channel_alert),
NotificationManager.IMPORTANCE_HIGH);
alerts.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
nm.createNotificationChannel(alerts);
// Contacts grouping
NotificationChannelGroup group = new NotificationChannelGroup(
"contacts",
getString(R.string.channel_group_contacts));
nm.createNotificationChannelGroup(group);
}
}
@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);
}