/** * 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(Build.VERSION_CODES.O) public static void createNotificationChannel(Context context, Account account) { if (AndroidHelper.isApi26OrGreater()) { final String defaultChannelName = context.getString( R.string.notifications_default_channel_name, account.getRepositoryDisplayName(), account.getAccountDisplayName()); final NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); nm.createNotificationChannelGroup(new NotificationChannelGroup( account.getAccountHash(), defaultChannelName)); NotificationChannel channel = new NotificationChannel(account.getAccountHash(), defaultChannelName, NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription(context.getString(R.string.notifications_default_channel_description)); channel.enableVibration(true); channel.enableLights(true); channel.setLightColor(ContextCompat.getColor(context, R.color.primaryDark)); channel.setShowBadge(true); channel.setGroup(account.getAccountHash()); nm.createNotificationChannel(channel); } }
public static void onCreate(Context context) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return; notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(GROUP_SERVICE, context.getString(R.string.notifications_group_service))); notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(GROUP_UPDATE, context.getString(R.string.notifications_group_updates))); NotificationChannel runningChannel = new NotificationChannel(SERVICE_RUNNING, context.getString(R.string.notifications_running), NotificationManager.IMPORTANCE_MIN); runningChannel.setDescription(context.getString(R.string.notifications_running_desc)); runningChannel.setGroup(GROUP_SERVICE); runningChannel.setShowBadge(false); notificationManager.createNotificationChannel(runningChannel); NotificationChannel pausedChannel = new NotificationChannel(SERVICE_PAUSED, context.getString(R.string.notifications_paused), NotificationManager.IMPORTANCE_LOW); pausedChannel.setDescription(context.getString(R.string.notifications_paused_desc)); pausedChannel.setGroup(GROUP_SERVICE); pausedChannel.setShowBadge(false); notificationManager.createNotificationChannel(pausedChannel); NotificationChannel updateChannel = new NotificationChannel(UPDATE_STATUS, context.getString(R.string.notifications_update), NotificationManager.IMPORTANCE_LOW); updateChannel.setDescription(context.getString(R.string.notifications_update_desc)); updateChannel.setGroup(GROUP_UPDATE); updateChannel.setShowBadge(false); notificationManager.createNotificationChannel(updateChannel); }
/** * Get list of Notification groups. * * @param context The application context. * @return Returns all notification groups. */ static List<NotificationChannelGroup> getNotificationGroups(Context context) { if (BuildUtil.isNotificationChannelSupported(context)) { NotificationManager notificationManager = (NotificationManager) context.getSystemService( Context.NOTIFICATION_SERVICE); if (notificationManager == null) { Log.e("Cannot get Notification Channel Groups, notificationManager is null."); return null; } return notificationManager.getNotificationChannelGroups(); } return null; }
@TargetApi(26) public static void cleanAllNotificationChannels() { // TODO this isn't right yet List<NotificationChannel> channels = getNotifManager().getNotificationChannels(); for (NotificationChannel channel : channels) { getNotifManager().deleteNotificationChannel(channel.getId()); } List<NotificationChannelGroup> groups = getNotifManager().getNotificationChannelGroups(); for (NotificationChannelGroup group : groups) { getNotifManager().deleteNotificationChannel(group.getId()); } }
private void setupNotificationChannel(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannelGroup timelineGroup = new NotificationChannelGroup( NotificationKey.CHANNEL_GROUP_TIMELINE, NotificationKey.CHANNEL_NAME_TIMELINE); NotificationChannelGroup friendGroup = new NotificationChannelGroup( NotificationKey.CHANNEL_GROUP_FRIEND, NotificationKey.CHANNEL_NAME_FRIEND); NotificationChannel friendConfirmationsChannel = new NotificationChannel( NotificationKey.CHANNEL_ID_FRIEND_CONFIRMATIONS, NotificationKey.CHANNEL_NAME_FRIEND_CONFIRMATIONS, NotificationManager.IMPORTANCE_DEFAULT); friendConfirmationsChannel.setGroup(NotificationKey.CHANNEL_GROUP_FRIEND); NotificationChannel friendRequestsChannel = new NotificationChannel( NotificationKey.CHANNEL_ID_FRIEND_REQUESTS, NotificationKey.CHANNEL_NAME_FRIEND_REQUESTS, NotificationManager.IMPORTANCE_DEFAULT); friendRequestsChannel.setGroup(NotificationKey.CHANNEL_GROUP_FRIEND); NotificationChannel photoTagsChannel = new NotificationChannel( NotificationKey.CHANNEL_ID_PHOTO_TAGS, NotificationKey.CHANNEL_NAME_PHOTO_TAGS, NotificationManager.IMPORTANCE_DEFAULT); photoTagsChannel.setGroup(NotificationKey.CHANNEL_GROUP_FRIEND); NotificationChannel commentChannel = new NotificationChannel( NotificationKey.CHANNEL_ID_COMMENT, NotificationKey.CHANNEL_NAME_COMMENT, NotificationManager.IMPORTANCE_DEFAULT); commentChannel.setGroup(NotificationKey.CHANNEL_GROUP_TIMELINE); NotificationChannel logInAlertChannel = new NotificationChannel( NotificationKey.CHANNEL_ID_LOG_IN_ALERT, NotificationKey.CHANNEL_NAME_LOG_IN_ALERT, NotificationManager.IMPORTANCE_DEFAULT); NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); manager.createNotificationChannelGroup(timelineGroup); manager.createNotificationChannelGroup(friendGroup); manager.createNotificationChannel(friendConfirmationsChannel); manager.createNotificationChannel(friendRequestsChannel); manager.createNotificationChannel(photoTagsChannel); manager.createNotificationChannel(commentChannel); manager.createNotificationChannel(logInAlertChannel); } }
@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); channel.setGroup(template.getGroup()); 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; }