LoginSignup
2
2

More than 5 years have passed since last update.

iOS アプリをブルブルさせてみる

Last updated at Posted at 2018-11-15

0.はじめに

ブルブルさせたいので、
既存のアプリにプッシュ通知(APNS)を実装してみます。

1.Mac の キーチェーンアクセス から、CSR ファイルを作成する。

基本的には、以下の記事の手順に沿って、CSR ファイルを作成します。

2.既存の App ID に「Push Notifications (Development)」を設定する。

  1. 以下のリンクにアクセスし、既存の App ID の情報を表示し、「Edit」ボタンを押下します。
  2. 「Push Notifications」にチェックを入れ、「Development SSL Certificate」の「Create Certificate」ボタンを押下します。
    • FireShot Capture 375 - Settings - iOS App IDs - Apple Develo_ - https___developer.apple.com_accoun.png
  3. 「Continue」ボタンを押下します。
    • FireShot Capture 376 - Add - iOS Cert_ - https___developer.apple.com_account_ios_certificate_create_.png
  4. 「Choose File ...」ボタンを押下し、作成した CSR ファイルをアップロードします。
    • FireShot Capture 377 - Add - iOS Cert_ - https___developer.apple.com_account_ios_certificate_create_.png
  5. 「Continue」ボタンを押下します。
    • FireShot Capture 378 - Add - iOS Cert_ - https___developer.apple.com_account_ios_certificate_create_.png
  6. 「Download」ボタンを押下し、CER ファイルをダウンロードし、「Done」ボタンを押下します。
    • ※プッシュ通知を送る時に必要。
    • FireShot Capture 379 - Add - iOS Cert_ - https___developer.apple.com_account_ios_certificate_create_.png
  7. 「Certificates」の一覧に作成された証明書が追加されます。
    • FireShot Capture 380 - iOS Certificates - Ap_ - https___developer.apple.com_account_ios_certificate_.png
  8. 「Identifiers」の一覧を表示し、既存の App ID の情報を確認します。
    • FireShot Capture 381 - iOS App IDs - Ap_ - https___developer.apple.com_account_ios_identifier_bundle.png

3.既存の プロビジョニングファイルを更新する。

  1. 以下のリンクにアクセスし、既存のプロビジョニングファイルの情報を確認します。
  2. 以下のページを参考に、警告が表示されているプロビジョニングファイルを更新します。

4.P12 ファイルを作成する。

基本的には、以下の記事の手順に沿って、P12 ファイルを作成します。

5.アプリの設定を変更する。

  1. 以下のページの手順に沿って、設定を変更します。

6.アプリのコードを変更する。

AppDelegate.swift

  1. import に以下を追加。
  2. import UserNotifications
    

  3. 以下のデリゲートを、継承元に追加。
  4. UNUserNotificationCenterDelegate
    

  5. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool メソッドに以下を追加。
  6.         let center = UNUserNotificationCenter.current()
            center.delegate = self
            center.requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { (granted, error) in
                if error != nil {
                    return
                }
                if granted {
                    debugPrint("通知許可")
                } else {
                    debugPrint("通知拒否")
                }
                DispatchQueue.main.async {
                    UIApplication.shared.registerForRemoteNotifications()
                }
            })
    

  7. 以下のメソッドを追加。
  8.     func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
            let tokenParts = deviceToken.map { data -> String in
                return String(format: "%02.2hhx", data)
            }
            let token = tokenParts.joined()
            print("Device Token: \(token)")
        }
    
        func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
            print(userInfo)
            completionHandler(.noData)
        }
    
        func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
            print(userInfo)
            completionHandler(.noData)
        }
    
        func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
            print("Failed to register for remote notifications with error: \(error)")
        }
    

7.プッシュ通知の受信を確認する。

Pusher というアプリがあるみたいで、そちらを使います。

  1. 以下のリンクの「README.md」の「Installation」の手順から、アプリをインストールします。
  2. アプリを起動すると通知の確認ダイアログが表示されるので、許可し、デバッグログからデバイストークンを確認します。
    • 2018-11-14_18-32-26_000.png
  3. Pusher を起動し、P12 ファイルをインポートします。
    • スクリーンショット 2018-11-15 10.22.23.png
  4. デバイストークンを入力し、「Push」ボタンを押下します。
    • スクリーンショット 2018-11-15 10.19.14.png
  5. アプリに通知が届くことを確認します。
    • 2018-11-14_18-36-10_000.png
    • アプリがフォアグラウンドの場合は、application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)のメソッドを通る形になるので、注意!!

99.ハマりポイント

  • 特に大きなハマりは無かったですが…。

  • この後、フォアグラウンド/バックグラウンド/未起動時などのそれぞれの状態での確認、通知の文言やレイアウトの調整、通知をクリックした後の動作など、諸々の対応が必要になるかと…。

XX.まとめ

また、
Amazon SNS を使っての通知など、確認しようかと考えています。

ご参考になれば♪

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