LoginSignup
220
214

More than 5 years have passed since last update.

【iOS】Firebase の Notifications でプッシュ通知を送る

Last updated at Posted at 2016-05-21

これは何

Firebase の Notifications でプッシュ通知を送った時のメモです。必要最低限のことしか書いていません。

実行環境

  • Xcode 7.3.1
  • Swift 2.2
  • CocoaPods 1.0.0
  • Firebase/Messaging 3.2.0

準備しておくもの

  • Googleアカウント(アカウントがあればすぐにFirebaseを使えます)
  • iOS実機(シミュレーター不可)
  • APNS証明書をp12形式で書き出したファイル

証明書の作り方は下記ドキュメントを参照して下さい。
Provisioning APNs SSL Certificates  |  Firebase

新規プロジェクトの作成

※ここで言うプロジェクトとはXcodeのプロジェクトではなく、Firebase上のプロジェクトを指します。

Firebase のコンソールを開いて、「新規プロジェクトを作成」をクリックします。

スクリーンショット 2016-05-21 0.40.06.png

プロジェクト名などを入力して「プロジェクトを作成」をクリックします。

スクリーンショット 2016-05-21 0.43.08.png

アプリの詳細を入力

プロジェクトを作成したら、「iOSアプリにFirebaseを追加」をクリックします。

スクリーンショット 2016-05-21 0.43.55.png

iOSアプリのバンドルID(例:com.yourapp.ios)などを入力していきます。App Store IDは任意です。ここで保存した設定ファイル(GoogleService-Info.plist)は後で使用します。

スクリーンショット 2016-05-21 0.44.30.png

Xcodeでプロジェクトを作成

Bundle Identifier が Firebase のコンソールで入力したバンドルIDと同じ内容になるようにします。

スクリーンショット 2016-05-21 13.03.36.png

プロジェクトを作成できたら、いったんXcodeは終了しておきます。

Firebase SDKをインストール

.xcodeproj ファイルがあるディレクトリで下記コマンドを実行します。

1) pod init
2) Podfile を編集

pod 'Firebase/Messaging'

3) pod install

インストールが完了したら、 .xcworkspace を開きます。

Push Notifications をオン

Capabilities の Push Notifications をオンにします。

スクリーンショット 2016-05-21 1.30.45.png

設定ファイルをコピー

先ほど作成した設定ファイル(GoogleService-Info.plist)をプロジェクトにコピーします。

スクリーンショット 2016-05-21 0.45.50.png

コードを実装

Firebase を import します。

import Firebase

次に AppDelegate の中で FIRApp.configure() を呼び出します。公式ドキュメントではこれだけで実装完了と書いてありましたが、私の環境では何故かうまくいきませんでした。(デバイストークンの取得に失敗してしまう)

ios - Cloud messaging handing terminate app - Stack Overflow

上記のQAを参考にし、最終的には以下のように実装する事でうまく動きました。Info.plist の FirebaseAppDelegateProxyEnabledNO にしています。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    let notificationSettings = UIUserNotificationSettings(
        forTypes: [.Badge, .Sound, .Alert], categories: nil)
    application.registerUserNotificationSettings(notificationSettings)
    application.registerForRemoteNotifications()
    FIRApp.configure()
    return true
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    if notificationSettings.types != .None {
        application.registerForRemoteNotifications()
    }
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var tokenString = ""

    for i in 0..<deviceToken.length {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }

    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)
}

プッシュ通知を送ってみる

Firebase のコンソールを開き、先ほど作成したプロジェクトを選択しておきます。

証明書を登録

プロジェクトの設定画面を開きます。

スクリーンショット 2016-05-21 1.10.35.png

あらかじめ用意しておいた p12 ファイルとパスワードを登録します。

スクリーンショット 2016-05-21 1.11.56.png

プッシュ送信

コンソールの左にあるメニューから「Notifications」を選び、「最初のメッセージを送信」をクリックします。

スクリーンショット 2016-05-21 2.11.28.png

メッセージ文などを入力し、「メッセージを送信」をクリックします。

スクリーンショット 2016-05-21 2.18.24.png

無事、プッシュ通知がきました!

image

リンク

220
214
9

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
220
214