LoginSignup
36
40

More than 5 years have passed since last update.

SwiftでFirebaseを使ってNotificationを送ってみた(が、色々詰まった話)

Last updated at Posted at 2016-05-23

若干話題に乗り遅れた感がありますが、SwiftでFirebaseを使ってNotificationを送ってみました。
ところどころ詰まったので、詰まったポイントを中心に記載しました。

環境

  • Swift: 2.2
  • CocoaPods: 1.0.0.rc.2
  • iOS: 8.0〜

1. FireBaseプロジェクトを作成

https://console.firebase.google.com/
googleアカウントでログインしてプロジェクトを作成する or 既存のプロジェクトを選択する

1.png

2. Xcodeプロジェクトを作成

適当な名前でXcodeプロジェクトを作成して下さい。

3. 「iOSアプリにFireBaseを追加」を選択

2.png

表示される手順に沿って初期設定を実施。

3.png

手順4の「初期化コードを追加」で詰まったので下記参考にPodfileを修正。CocoaPodsのバージョンの問題?それとも公式の手順の更新漏れ?

iOS - Firebase error: Use of unresolved identifier FIRApp running supplied codeを参考にPodfileを修正。
ついでに、Messaging用のライブラリも入れておく。

Podfile
...
pod 'Firebase/Core'
pod 'Firebase/Messaging'
...

4. APNs 証明書の準備

Notificationを送信するにはAPNs 証明書が必要なため準備します。

4-1APNs 証明書を作成

Provisioning APNs SSL Certificates
「APNs 証明書」の検索結果 - Qiita
あたりの手順を参考に証明書を作成して下さい。

4-2. APNs 証明書をアップロード

作成した証明書をFirebaseにアップロードして下さい。

4.png

5.png

5. アプリの設定

単純にNotificationを送るだけであれば、FireBaseの初期化時 FIRApp.configure() によしなに設定しておいてくれるので、特にコードを書く必要はないそうです。親切設計ですね。

・・・のはずが、notificationが受信できない。

Xcodeのコンソールを見てみると

<FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)"

てなエラーが出てtokenの取得に失敗している模様。

"Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001"で検索しても2件しか引っかからない・・・。

こちらの回答を参考に自分でnotificationの設定をしてみた。
とりあえずは動くようにはなったものの腑に落ちない。

あ、あと、うっかりシミュレータで起動すると下記のエラーが出ました。notificationは実機じゃないと受信できないので、実機に向けて向けてビルドしましょう。

<FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=NSCocoaErrorDomain Code=3010 "REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED_NSERROR_DESCRIPTION" UserInfo={NSLocalizedDescription=REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED_NSERROR_DESCRIPTION}

6. notificationを送ってみる

さて、ようやく準備が出来たのでnotificationを送ってみましょう。が、1点注意点があります。

Note: unless you first add message handling logic to your client app as described in the next section, some users may not receive the notifications you send. This includes any users who have your app in the foreground on the device.
参考:Send a Notification to a User Segment on iOS

公式のドキュメントには上記のように記載があり、ここまでの実装状況ではアプリがforegroundにあるときに受けとったメッセージはハンドリングしていないので、後述するようなロジックを追加してあげる必要があります。

まずは、アプリがforegroundじゃない状態でnotificationを送ってみましょう。
実機にアプリをインストールして、通知を許可した後、Firebaseのコンソールからnotificationを送ります。

スクリーンショット_2016-05-23_22_49_36.png

スクリーンショット_2016-05-23_22_50_34.png

とりあえず、メッセージ文とターゲットだけ決めれば送信できます。

6-1. ロック画面の時に送信

2016-05-23_19_55_12.png

6-2. アプリがbackgroundにある時に送信

2016-05-23_22_40_55.png

6-3. アプリ終了後に送信

2016-05-23_22_41_43.png

ふむ、良いですね。

7. 受信したnotificationの内容をアプリ上で扱うには

アプリがforegroundにある時に受信したメッセージの内容を扱いたい場合には下記のような感じでAppDelegateの application:didReceiveRemoteNotification を実装すればOKです。
今回はとりあえずnotificationを受信したらalertを表示するようにしてみました。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    if application.applicationState == .Active {
        if let aps = userInfo["aps"] as? NSDictionary {
            if let alert = aps["alert"] as? String {
                let alert = UIAlertController(title: "受け取ったメッセージは", message: alert, preferredStyle: .Alert)
                alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
                self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
            }
        }
    }
}

さて、上記を実装して、アプリがforegroundにある時に、再度notificationを送ってみましょう。

2016-05-23_19_46_39.png

うん、良さそうですね。

無料で簡単にいくらでもnotificationが送れるらしい、ということで飛びついてみたら、公式のドキュメント通りにやってみたつもりが、自分の書き方が悪いのか、設定が抜けてるのか、意外と引っかかった部分が多かったです。
FIRApp.configure() の実行だけでnotificationを送れた方がいたら、環境や手順などを公開されることを期待してます!

36
40
1

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
36
40