android.app.NotificationManager#getNotificationChannels ( )源码实例Demo

下面列出了android.app.NotificationManager#getNotificationChannels ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: mollyim-android   文件: NotificationChannels.java
@TargetApi(26)
@WorkerThread
public static synchronized void ensureCustomChannelConsistency(@NonNull Context context) {
  if (!supported()) {
    return;
  }
  Log.d(TAG, "ensureCustomChannelConsistency()");

  NotificationManager notificationManager = ServiceUtil.getNotificationManager(context);
  RecipientDatabase   db                  = DatabaseFactory.getRecipientDatabase(context);
  List<Recipient>     customRecipients    = new ArrayList<>();
  Set<String>         customChannelIds    = new HashSet<>();

  try (RecipientDatabase.RecipientReader reader = db.getRecipientsWithNotificationChannels()) {
    Recipient recipient;
    while ((recipient = reader.getNext()) != null) {
      customRecipients.add(recipient);
      customChannelIds.add(recipient.getNotificationChannel());
    }
  }

  Set<String> existingChannelIds  = Stream.of(notificationManager.getNotificationChannels()).map(NotificationChannel::getId).collect(Collectors.toSet());

  for (NotificationChannel existingChannel : notificationManager.getNotificationChannels()) {
    if (existingChannel.getId().startsWith(CONTACT_PREFIX) && !customChannelIds.contains(existingChannel.getId())) {
      Log.i(TAG, "Consistency: Deleting channel '"+ existingChannel.getId() + "' because the DB has no record of it.");
      notificationManager.deleteNotificationChannel(existingChannel.getId());
    } else if (existingChannel.getId().startsWith(MESSAGES_PREFIX) && !existingChannel.getId().equals(getMessagesChannel(context))) {
      Log.i(TAG, "Consistency: Deleting channel '"+ existingChannel.getId() + "' because it's out of date.");
      notificationManager.deleteNotificationChannel(existingChannel.getId());
    }
  }

  for (Recipient customRecipient : customRecipients) {
    if (!existingChannelIds.contains(customRecipient.getNotificationChannel())) {
      Log.i(TAG, "Consistency: Removing custom channel '"+ customRecipient.getNotificationChannel() + "' because the system doesn't have it.");
      db.setNotificationChannel(customRecipient.getId(), null);
    }
  }
}
 
/**
 * Get list of NotificationChannel.
 *
 * @param context The application context.
 * @return Returns all notification channels belonging to the calling package.
 */
static List<NotificationChannel> getNotificationChannels(Context context) {
  if (BuildUtil.isNotificationChannelSupported(context)) {
    NotificationManager notificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (notificationManager == null) {
      Log.e("Notification manager is null");
      return null;
    }
    return notificationManager.getNotificationChannels();
  }
  return null;
}
 
源代码3 项目: Android-SDK   文件: PushTemplateHelper.java
static public void deleteNotificationChannel( Context context )
{
  if( android.os.Build.VERSION.SDK_INT < 26 )
    return;

  NotificationManager notificationManager = (NotificationManager) context.getSystemService( Context.NOTIFICATION_SERVICE );
  List<NotificationChannel> notificationChannels = notificationManager.getNotificationChannels();
  for (NotificationChannel notifChann : notificationChannels)
    notificationManager.deleteNotificationChannel( notifChann.getId() );
}
 
@RequiresApi(Build.VERSION_CODES.O)
private static boolean hasChannels(@NonNull final NotificationManager notificationManager) {
    final List<NotificationChannel> channels = notificationManager
            .getNotificationChannels();
    return channels != null && !channels.isEmpty();
}