下面列出了android.app.NotificationManager#deleteNotificationChannel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void checkAndCreateNotificationChannel(Context context) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
return;
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = notificationManager.getNotificationChannel("yourLocalWeather");
boolean createNotification = notificationChannel == null;
if (!createNotification &&
((notificationChannel.getImportance() == NotificationManager.IMPORTANCE_LOW) ||
(AppPreference.isVibrateEnabled(context) && (notificationChannel.getVibrationPattern() == null)))) {
notificationManager.deleteNotificationChannel("yourLocalWeather");
createNotification = true;
}
if (createNotification) {
NotificationChannel channel = new NotificationChannel("yourLocalWeather",
context.getString(R.string.notification_channel_name),
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(context.getString(R.string.notification_channel_description));
channel.setVibrationPattern(isVibrateEnabled(context));
channel.enableVibration(AppPreference.isVibrateEnabled(context));
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channel.setSound(null, null);
notificationManager.createNotificationChannel(channel);
}
}
@TargetApi(26)
private static void onUpgrade(@NonNull NotificationManager notificationManager, int oldVersion, int newVersion) {
Log.i(TAG, "Upgrading channels from " + oldVersion + " to " + newVersion);
if (oldVersion < Version.MESSAGES_CATEGORY) {
notificationManager.deleteNotificationChannel("messages");
notificationManager.deleteNotificationChannel("calls");
notificationManager.deleteNotificationChannel("locked_status");
notificationManager.deleteNotificationChannel("backups");
notificationManager.deleteNotificationChannel("other");
}
if (oldVersion < Version.CALLS_PRIORITY_BUMP) {
notificationManager.deleteNotificationChannel("calls_v2");
}
}
private void exitForeground(){
synchronized (NOTIFICATION_LOCK) {
if (isForeground && !isPrimaryTransportConnected()) { //Ensure that the service is in the foreground and no longer connected to a transport
DebugTool.logInfo("SdlRouterService to exit foreground");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
this.stopForeground(Service.STOP_FOREGROUND_DETACH);
}else{
stopForeground(false);
}
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager!= null){
try {
notificationManager.cancelAll();
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !getBooleanPref(KEY_AVOID_NOTIFICATION_CHANNEL_DELETE,false)) {
notificationManager.deleteNotificationChannel(SDL_NOTIFICATION_CHANNEL_ID);
}
} catch (Exception e) {
DebugTool.logError("Issue when removing notification and channel", e);
}
}
isForeground = false;
}
}
}
public static void init(@NonNull Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String name = "Book downloads";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(ID, name, importance);
channel.setSound(null, null);
channel.setVibrationPattern(null);
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
// Mandatory; it is not possible to change the sound of an existing channel after its initial creation
Objects.requireNonNull(notificationManager, "notificationManager must not be null");
notificationManager.deleteNotificationChannel(ID_OLD);
notificationManager.createNotificationChannel(channel);
}
}
/**
* Delete push notification channel.
*
* @param context The application context.
* @param channelId The id of the channel.
*/
private static void deleteNotificationChannel(Context context, String channelId) {
if (context == null) {
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.deleteNotificationChannel(channelId);
} catch (Throwable t) {
Util.handleException(t);
}
}
}
public static void init(@NonNull Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String name = "Updates";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(ID, name, importance);
channel.setSound(null, null);
channel.setVibrationPattern(null);
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
// Mandatory; it is not possible to change the sound of an existing channel after its initial creation
Objects.requireNonNull(notificationManager, "notificationManager must not be null");
notificationManager.deleteNotificationChannel(ID_OLD);
notificationManager.deleteNotificationChannel(ID_OLD2);
notificationManager.createNotificationChannel(channel);
}
}
public static void init(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String name = "Technical maintenance";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(ID, name, importance);
channel.setSound(null, null);
channel.setVibrationPattern(null);
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
// Mandatory; it is not possible to change the sound of an existing channel after its initial creation
Objects.requireNonNull(notificationManager, "notificationManager must not be null");
notificationManager.deleteNotificationChannel(ID_OLD);
notificationManager.createNotificationChannel(channel);
}
}
/**
* Deletes the channel generated for the provided recipient. Safe to call even if there was never
* a channel made for that recipient.
*/
public static synchronized void deleteChannelFor(@NonNull Context context, @NonNull Recipient recipient) {
if (!supported()) {
return;
}
NotificationManager notificationManager = ServiceUtil.getNotificationManager(context);
String channel = recipient.getNotificationChannel();
if (channel != null) {
Log.i(TAG, "Deleting channel");
notificationManager.deleteNotificationChannel(channel);
}
}
@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);
}
}
}
@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);
}
}
@Override
public void onDestroy() {
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.deleteNotificationChannel("channel_01");
notificationManager.deleteNotificationChannel("channel_02");
}
isEqualizerSet = false;
if (PlayerActivity2.activity!=null) {
PlayerActivity2.activity.finish();
}
try {
bottom_player.setVisibility(View.GONE);
adView.setVisibility(View.GONE);
onClear();
MainActivity.activity.findViewById(R.id.adViewLayout_add)
.setVisibility(View.GONE);
}catch (Exception ignored){ }
notificationManagerCompat.cancel(1);
try {
if (mEqualizer!=null) mEqualizer.release();
if (bassBoost!=null) bassBoost.release();
if (loudnessEnhancer!=null) loudnessEnhancer.release();
if (virtualizer!=null) virtualizer.release();
player.stop();
player.release();
}catch (Exception e) { e.printStackTrace(); }
wakeLock.release();
activity = null;
super.onDestroy();
}
public void disableContext(Context context) {
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
notificationManagerCompat.cancel(1);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.deleteNotificationChannel("channel_01");
}
Process.killProcess(Process.myPid());
}
public void deleteNotificationChannel()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationManager Manager = mActivityInstance.getSystemService(NotificationManager.class);
Manager.deleteNotificationChannel(NOTIFICATION_CHANNEL_ID);
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
public static void createChannel(@NonNull Context context) {
Precondition.checkNotNull(context);
final NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT);
channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
final NotificationManager manager = getManager(context);
manager.deleteNotificationChannel(CHANNEL_ID);
manager.createNotificationChannel(channel);
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) {
int position = viewHolder.getAdapterPosition();
int index = position - getUserRulesStartIndex();
NotificationRule rule = mRules.remove(index);
notifyItemRemoved(position);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mgr = (NotificationManager) viewHolder.itemView.getContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
mgr.deleteNotificationChannel(rule.settings.notificationChannelId);
}
if (mRules.size() == 0)
notifyItemInserted(getUserRulesStartIndex() + 1); // the tip
Snackbar.make(viewHolder.itemView, R.string.notification_custom_rule_deleted, Snackbar.LENGTH_SHORT)
.setAction(R.string.action_undo, (View v) -> {
int newIndex = Math.min(index, mRules.size());
mRules.add(newIndex, rule);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
ChannelNotificationManager.createChannel(
viewHolder.itemView.getContext(), rule);
if (mRules.size() == 1)
notifyItemRemoved(getUserRulesStartIndex() + 1); // the tip
notifyItemInserted(newIndex + getUserRulesStartIndex());
mHasChanges = true;
})
.show();
mHasChanges = true;
}
@ReactMethod
public void deleteNotificationChannel(String id, Promise promise) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mngr = (NotificationManager) getReactApplicationContext().getSystemService(NOTIFICATION_SERVICE);
mngr.deleteNotificationChannel(id);
}
promise.resolve(null);
}
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(api = Build.VERSION_CODES.O)
void deleteNotificationChannel(Context context) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.deleteNotificationChannel(getNotificationChannelId(id));
}
@RequiresApi(api = Build.VERSION_CODES.O)
void deleteNotificationChannel(Context context) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.deleteNotificationChannel(getNotificationChannelId(id));
}
private void destroyNotificationChannel(String channelId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.deleteNotificationChannel(channelId);
}
}