LoginSignup
8
8

More than 5 years have passed since last update.

Parseを触ってみた(iOS-3) - Push Notification -

Last updated at Posted at 2015-05-06

今までの記事

今回試したこと - Push Notification

Parse(MBasS)を使うことの強い理由の1つと思われるPush Notificationサポート。
どれぐらい簡単なのかを試してみた。

設定

やるべきことは、「Apple側の設定」「Parse側の設定」「アプリのコード記述」の3つ。

iOS Dev Centerでの設定

これはPush Notificationを使うならParse関係なく必ずやること。

App ID(Identifiers)の作成

  • 新規にApp IDを作成する場合
    • 作成時に「App Services」のPush Notificationsにチェックを入れて作成する。
  • すでに作成済みのApp IDを使う場合
    • Editから「Push Notifications」にチェックを入れて編集をしておく。

作成が終わると「App ID」の「Push Notifications」が以下のようにConfigurableになっている。

スクリーンショット 2015-05-06 15.19.51.png

通知用証明書の作成

CSR file

開発をおこなっているMacからキーチェンアクセスを起動する。
そして「キーチェンアクセス」から「証明書アシスタント」の「認証局に証明書を要求」を実行して、ファイル(CSR)をディスクに保存しておく。

証明書の作成

「Apple Dev Center」の「Certificates」から証明書を新規に作成する。

  • Developmentであれば以下を選択する。
    • Apple Push Notification service SSL (Sandbox)
  • Productionであれば以下を選択する。
    • Apple Push Notification service SSL (Production)

この作成時に、保存しておいたCSR fileをアップロードして作成する。

作成が終わると「App ID」の「Push Notifications」が以下のようにEnabledになっている。

スクリーンショット 2015-05-06 15.30.43.png

作成した証明書はダウンロードし、読み込んでおく。

プロビジョニングプロファイルの作成

用意した「App ID」を元にプロビジョニングプロファイルを作成しておく。
作成したプロビジョニングプロファイルはダウンロードし、読み込んでおく。

Parseの設定

「Settings」の「Push」を開く。

Apple Push Certificates

開発をおこなっているMacからキーチェンアクセスを起動し、今回作成した証明書を選択しファイル(p12ファイル)を書き出す。
(注意)ここでパスワードを設定しておかないようにしておく。

書きだしたファイルは以下からアップロードする。
スクリーンショット 2015-05-06 15.53.23.png

アップロードすると以下のように情報が表示される。
スクリーンショット 2015-05-06 15.54.26.png

Push Notification Settings

「Client push enabled?」をYesにしておく。

コード記述

AppDelegate.mに以下のように記述する。
「ParseAppID(Application ID)」と「ParseClientKey(Client Key)」はParseの「Settings」の「Keys」にある値を使う。

サンプルコード
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [Parse setApplicationId:ParseAppID
                  clientKey:ParseClientKey];

    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                    UIUserNotificationTypeBadge |
                                                    UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                             categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];

    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    currentInstallation.channels = @[ @"global" ];
    [currentInstallation saveInBackground];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [PFPush handlePush:userInfo];
}

※上記コードはiOS8以降のみ対応になります。

これで準備は完了。
あとは色々な手段でプッシュ通知をするだけ。

プッシュ通知

プッシュ通知は、「ParseのWeb」「REST API」「アプリ」「Cloud Code」から送ることが出来る。

ParseのWeb

「Push」から送りたいプッシュ通知の内容を設定するだけでOK。

スクリーンショット 2015-05-06 16.09.25.png

REST API

以下のコマンドを叩くだけでOK。

curl -X POST \
  -H "X-Parse-Application-Id: ${APPLICATION_ID}" \
  -H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
        "where": {
          "deviceType": "ios"
        },
        "data": {
          "alert": "Hello World!"
        }
      }' \
  https://api.parse.com/1/push

Push通知一覧画面

設定したPush通知の一覧は以下のように表示される。

スクリーンショット 2015-05-06 16.25.48.png

まとめ

Push Notificationを自前で全てやろうとすると非常に面倒(運用も含めて)なので、諦めている個人開発者も多いはず。
しかし、たったこれだけの設定で利用出来るだけでなく、色々とカスタマイズ出来るというのは非常にありがたい。

そして、値段も100万端末/月なので、利用しない手はないでしょう。

今回、紹介しきれなかった「チャンネル」の話や他のプッシュ通知の方法については、また別の機会にアップします。

参考

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