LoginSignup
6
4

More than 5 years have passed since last update.

【Androidアプリ】通知チャンネルの通知設定を取得する

Last updated at Posted at 2018-07-24

前回の記事で「端末でアプリの通知をオフに設定してもダイアログが表示されてしまう」から、「通知設定を取得してオンの場合のみ、ダイアログを表示するように」した、と書きました。
ただし、Android Oreoで通知チャンネルを2つ以上つくった場合、通知チャンネルの通知設定で判定する必要があります。

通知チャンネルの通知設定を取得する

「通知チャンネル オン オフ 取得」でググったところ、下記の記事がヒット。
#DroidKaigi 2018で「開発者が知っておきたい通知の歴史」という内容で講演しました - kobakei's blog http://kobakei.hatenadiary.jp/entry/2018/02/09/223057

確かに公式ドキュメントを読んでみるとそんな感じのことが英語で書いてありました。

ということで、上記の公式ドキュメントを参考に実装したのが下記になります。

2018/07/26 追記
アプリの通知設定がオフ、通知チャンネルの通知設定がオンの場合にダイアログが表示されてしまったのでコードを修正しました

2018/09/06 追記
notificationManager.getNotificationChannel(CHANNEL_ID)
↑ 通知チャンネル作成できてないとぬるぽになります

sample.java
boolean isPermitNotification;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    final String CHANNEL_ID = getString(R.string.channel_id);
    isPermitNotification = notificationManagerCompat.areNotificationsEnabled()
        && NotificationManager.IMPORTANCE_NONE != notificationManager.getNotificationChannel(CHANNEL_ID).getImportance();

} else {
    isPermitNotification = notificationManagerCompat.areNotificationsEnabled();

}

通知チャンネル、ようやくちょっと分かってきた気がします。ユーザ側で通知が色々設定できていいですね。

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4