3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Swift] ローカル通知と通知アクションの呼ばれ方

Last updated at Posted at 2017-06-29

#[Swift] ローカル通知と通知アクションの呼ばれ方

import UserNotifications

//ローカル通知クラスとして実装
class NotificationManager: NSObject, UNUserNotificationCenterDelegate {

    //シングルトン
    static let shared = NotificationManager()
    
    private override init() {
        super.init()
        regist()
    }
    
    func regist() {
        //デリゲート設定
        UNUserNotificationCenter.current().delegate = self
        //通知利用の登録
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
            (granted, error) in
            // Error
        }
    }
    
    //フォアグラウンド時の通知
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert, .sound])
    }
    
    //通知タップ
    //以下の場合呼ばればい
    //・通知をタップしない場合
    //・通知を×ボタンで閉じた場合
    //・ロック画面で消去をタップした場合
    //・フォアグラウンド指定なしでアプリが起動していない場合
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        completionHandler()
    }
    
    func add() {     
        //フォアグラウンド指定なしなのでアプリが起動していない場合は通知タップが呼ばれない   
        let action1 = UNNotificationAction(identifier: "action1", title: "action1", options: [])
        
        //フォアグラウンド指定ありなのでアプリが起動していない場合も通知タップが呼ばれる
        let action2 = UNNotificationAction(identifier: "action2", title: "action2", options: [.foreground])

        let category = UNNotificationCategory(identifier: "myCategory", actions: [action1, action2], intentIdentifiers: ["action1", "action2"] , options: [])       
        UNUserNotificationCenter.current().setNotificationCategories([category])
        
        let content = UNMutableNotificationContent()
        content.title = NSString.localizedUserNotificationString(forKey: "title", arguments: nil)
        content.body = NSString.localizedUserNotificationString(forKey: "body", arguments: nil)
        content.sound = UNNotificationSound.default()
        content.categoryIdentifier = "myCategory"
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
        let request = UNNotificationRequest(identifier: "myNotification", content: content, trigger: trigger)

        //通知の追加
        UNUserNotificationCenter.current().add(request) { (error : Error?) in
            if error != nil {
                // Error
            }
        }
    }
}
3
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?