2017-09-14 3 views
6

コードをAPI v26に更新し、NotificationChannelsを設定しました。私の通知が表示されますが、無効な通知に関するロジックがあります。 26の前に私のようなものがあります:NotificationChannelがユーザ(API 26)によって無効にされた場合の定義方法は?

NotificationManagerCompat.from(context).areNotificationsEnabled() 

をそして、これは今ではない有用であると思われます。では、設定で通知チャンネルが無効になっているかどうかをどのように知ることができますか?

+1

通知チャネルの[重要度](https://developer.android.com/reference/android/app/NotificationChannel.html#getImportance())が[IMPORTANCE_NONE](https://developer.android.com/reference)になることがあります/android/app/NotificationManager.html#IMPORTANCE_NONE)?私は確信していない、ちょうど仮定しています。 – azizbekian

+1

@azizbekian仮定してくれてありがとう、それはあなたが正しいことに変わります。 –

答えて

7

私は新しいことがわかりましたChannelNotificationアプローチは古いロジックに取って代わるものではなく、通知のコントロールのレイヤーをもう1つ追加します。 は、だから今、私たちは2つのシナリオを持って、スクリーンショットを参照してください。

enter image description here

  1. あなたは通知が有効になっている場合に定義することができます

    NotificationManagerCompat.from(context).areNotificationsEnabled(); 
    
  2. あなたの通知がユーザーに表示されている場合は、定義することができます。

    NotificationManager notificationManager = (NotificationManager) 
        context.getSystemService(Context.NOTIFICATION_SERVICE);  
        NotificationChannel notificationChannel = 
        notificationManager.getNotificationChannel(channelId);  
        int importance = notificationChannel.getImportance(); 
    

重要度がNotificationManager.IMPORTANCE_NONEの場合、通知は表示されませんが、通知があるので、フォアグラウンドサービスで使用でき、通知を却下する必要があります。重要度がNotificationManager.IMPORTANCE_MIN以上の場合は通知が表示されます。

関連する問題