LoginSignup
81

More than 5 years have passed since last update.

通知設定を取得する

Last updated at Posted at 2016-09-27

はじめに

iOS10より通知設定が取得できるようなりましたので、試してみました。
各ユーザの通知設定を判断し、何かしらのアクションができそうですね。

※取得可能な情報は、下記のとおりです。

  1. 「通知を許可」の可否
  2. 「通知センターに表示」の有無
  3. 「Appアイコンにバッジを表示」の有無
  4. 「ロック画面に表示」の有無
  5. 「通知スタイル」の種類
  6. サウンドの可否
  7. バッジの可否
  8. アラートの可否

スクリーンショット 2016-09-27 13.02.19.png

ただし、iOS9以下でも、以下のように通知許可の可否は判断できます。

AppDelegate.swift
    func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {

        if notificationSettings.types == .none {
            print("通知を不許可")
        } else {
            //.badge / .sound / .alert
            print("通知を許可")
        }
    }

1. 「通知を許可」の可否

通知設定の「通知を許可」の値が取得できます。

authorizationStatus 説明
.notDetermined 未選択
.denied 不許可
.authorized 許可

値は、下記のようなenumで定義されています。

UNNotificationSettings.h
@available(iOS 10.0, *)
public enum UNAuthorizationStatus : Int {


    // The user has not yet made a choice regarding whether the application may post user notifications.
    case notDetermined


    // The application is not authorized to post user notifications.
    case denied


    // The application is authorized to post user notifications.
    case authorized
}

設定の取得方法は、下記のとおりです。
なお、利用の際は、import UserNotificationsをお忘れなく!

使い方
        UNUserNotificationCenter.current().getNotificationSettings { (settings) in

            switch settings.authorizationStatus {
            case .authorized:
                break
            case .denied:
                break
            case .notDetermined:
                break
            }
        }

2. 「通知センターに表示」の有無

通知設定の「通知センターに表示」の値が取得できます。

notificationCenterSetting 説明
.notSupported 未サポート
.disabled OFF
.enabled ON

値は、下記のようなenumで定義されています。

UNNotificationSettings.h
@available(iOS 10.0, *)
public enum UNNotificationSetting : Int {

    // The application does not support this notification type
    case notSupported


    // The notification setting is turned off.
    case disabled


    // The notification setting is turned on.
    case enabled
}

設定の取得方法は、下記のとおりです。

使い方
        UNUserNotificationCenter.current().getNotificationSettings { (settings) in

            switch settings.notificationCenterSetting {
            case .enabled:
                break
            case .disabled:
                break
            case .notSupported:
                break
            }
        }

3. 「Appアイコンにバッジを表示」の有無

通知設定の「Appアイコンにバッジを表示」の値が取得できます。

badgeSetting 説明
.notSupported 未サポート
.disabled OFF
.enabled ON

4. 「ロック画面に表示」の有無

通知設定の「ロック画面に表示」の値が取得できます。

lockScreenSetting 説明
.notSupported 未サポート
.disabled OFF
.enabled ON

5. 「通知スタイル」の種類

通知設定の「通知スタイル」の値が取得できます。

alertStyle 説明
.none なし
.banner バナー
.alert アラート

値は、下記のようなenumで定義されています。

UNNotificationSettings.h
@available(iOS 10.0, *)
public enum UNAlertStyle : Int {

    case none

    case banner

    case alert
}

設定の取得方法は、下記のとおりです。

サンプル
        UNUserNotificationCenter.current().getNotificationSettings { (settings) in

            switch settings.alertStyle {
            case .none:
                break
            case .banner:
                break
            case .alert:
                break
            }
        }

6. サウンドの可否

UNUserNotificationCenter.current().requestAuthorizationにて、
オプションに設定した値(.sound)が取得できます。

soundSetting 説明
.notSupported 未サポート
.disabled OFF
.enabled ON

7. バッジの可否

UNUserNotificationCenter.current().requestAuthorizationにて、
オプションに設定した値(.badge)が取得できます。

badgeSetting 説明
.notSupported 未サポート
.disabled OFF
.enabled ON

8. アラートの可否

UNUserNotificationCenter.current().requestAuthorizationにて、
オプションに設定した値(.alert)が取得できます。

alertSetting 説明
.notSupported 未サポート
.disabled OFF
.enabled ON

まとめ

通知をOFFにしているユーザに対して、
OSが表示する通知許可ダイアログを
再度表示できるようにしてほしいですね。

下記のダイアログのことです。

スクリーンショット 2016-09-27 13.39.45.png

もし、できるようでしたら、ご教授頂ければ幸いです。

参考

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
81