[英]How can I know programmatically if a notification channel is enabled on Android O?
查看文檔,我可以看到檢查通知通道所有屬性的方法,但我找不到檢查通道本身是啟用還是禁用的方法。
我錯過了什么嗎?
官方文檔有你的答案:
您可以調用以下兩種方法來發現用戶已應用於通知渠道的設置:
- o 檢索單個通知通道,您可以調用
getNotificationChannel()
。- 要檢索屬於您的應用的所有通知渠道,您可以調用
getNotificationChannels()
。擁有 NotificationChannel 后,您可以使用諸如
getVibrationPattern()
和getSound()
來找出用戶當前擁有的設置。 要了解用戶是否阻止了通知渠道,您可以調用getImportance()
。 如果通知通道被阻塞,getImportance()
返回IMPORTANCE_NONE 。
所以getImportance()
會告訴你通知通道是否被阻止。
這是我的完整代碼:
public static boolean isNotificationChannelEnabled(@NonNull String groupId, @NonNull String... channelIds) {
boolean appNotificationEnable = NotificationManagerCompat.from(AppContext.getContext()).areNotificationsEnabled();
if (!appNotificationEnable) {
return false;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager) AppContext.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
List<NotificationChannelGroup> groupList = manager.getNotificationChannelGroups();
for (NotificationChannelGroup group : groupList) {
if (TextUtils.equals(group.getId(), groupId)) {
if (group.isBlocked()) {
return false;
}
}
}
}
for (String channelId : channelIds) {
NotificationChannel channel = manager.getNotificationChannel(channelId);
if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
return false;
}
}
return true;
}
return false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.