LoginSignup
2
1

More than 5 years have passed since last update.

プッシュ通知を表示する(UIKit::UIApplication.registerForRemoteNotifications)

Posted at

リモート通知(プッシュ通知)表示

記事が対象としている読者的に、mBaaSの使用を前提
プッシュ通知の送れるmBaaSの例

プッシュ通知は、サーバによってApple Push Notificationサービス(APNs)に送信され、そこからデバイスに配信(プッシュ)
ユーザにとってはローカル通知もプッシュ通知も同じに見える

通知タイプ登録

通知のタイプを何も登録しなければ、システムは画面に何も表示することなく、リモート通知をアプリケーションにプッシュする

AppDelegate.swift
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationDidFinishLaunching(application: UIApplication) {
        let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)

        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    }

    func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
        print(notificationSettings.types)
    }

デバイストークン登録

プッシュ通知受領登録

AppDelegate.swift
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()

デバイストークン取得・保存・登録

AppDelegate.swift
        print(notificationSettings.types)
    }

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        // Save device token and Send it to provider
    }

    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        print(error.description)
    }

デバイストークンは変化することがあるので、アプリケーションは起動の都度、登録し直す必要がある

アプリケーションがフォアグラウンドで動作中に通知を受け取った場合の処理

AppDelegate.swift
        print(error.description)
    }

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        // Handle notification
    }

アプリケーションがフォアグラウンドで動作中以外に通知を受け取った場合の処理

AppDelegate.swift
    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        // Handle notification
    }

    func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
        print("Identifier is \(identifier)")
    }

通知をタップしてアプリケーションが起動された場合の処理

AppDelegate.swift
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        if let options = launchOptions {
            let payload = options[UIApplicationLaunchOptionsRemoteNotificationKey] as! NSDictionary
            if let data = payload.objectForKey("key") {
                print("data for key: \(data)")
            }
        }

参考情報

Local および Push Notification プログラミングガイド

2
1
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
2
1