エンジニアとしての市場価値を測りませんか?PR

企業からあなたに合ったオリジナルのスカウトを受け取って、市場価値を測りましょう

7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

nifty cloud mobile backendでPush通知を実装する

Last updated at Posted at 2016-08-02

nifty cloud mobile backendとは

ニフティクラウドmobile backend

誠に勝手ながら nifty cloud mobile backend って毎回入力するのが面倒なので以降は「 mBaaS 」とします。

mBaaSとは
スマートフォンアプリのバックエンド開発が
開発不要になるクラウドサービス

スクリーンショット 2016-08-02 午後7.17.30.png

引用:http://mb.cloud.nifty.com/about.htm

準備

  1. XCodeプロジェクトの新規作成

  2. CocoaPodsでのSDK導入

    Podfile

pod 'NCMB', :git => 'https://github.com/NIFTYCloud-mbaas/ncmb_ios.git'

```bash
$ pod install
$ open PROJECT_NAME.xcworkspace

iOSアプリ側実装

プッシュ通知承諾アラート

ここのアラートを出すのはアプリによってマチマチだと思います。実際はどこでもいいです!
チャットアプリだったら初めてメッセージ画面に移動したときにアラートを表示させるようにしてもいいです。

AppDelegate.m
# import <NCMB/NCMB.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [NCMB setApplicationKey:@"アプリケーションキー" clientKey:@"クライアントキー"];
       
    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
        //iOS8以上
        UIUserNotificationType type = UIUserNotificationTypeAlert |
                                      UIUserNotificationTypeBadge |
                                      UIUserNotificationTypeSound;
        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:type
                                                                                   categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        //iOS7以下
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert |
                                                                                UIRemoteNotificationTypeBadge |
                                                                                UIRemoteNotificationTypeSound)];
    }
}

また、 mBaaS のリポジトリに適当すぎるほどのプルリクを出しているのでマージされればここが1行で済むようになります。

スクリーンショット_2016-08-02_午後7_50_41.png

この設定を忘れずに!!画像ではオフになっていますが、オンにしてください!!

デバイストークン取得後

AppDelegate.m
- (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NCMBInstallation *installation = [NCMBInstallation currentInstallation];
    //Device Tokenを設定
    [installation setDeviceTokenFromData:deviceToken];
    
    //端末情報をデータストアに登録
    __weak typeof (self) weakSelf = self;
    [installation saveInBackgroundWithBlock:^(NSError *error) {
        if(error){
            //端末情報の登録が失敗した場合の処理
            NSLog(@"Failed: %@", error);
            if (error.code == 409001) {
                //失敗した原因がdeviceTokenの重複だった場合、上書き処理を行う
                [weakSelf updateExistInstallation:installation];
            } else {
                //deviceTokenの重複以外のエラーが返ってきた場合
                NSLog(@"%@", error);
            }
            
        } else {
            //端末情報の登録が成功した場合の処理
            NSLog(@"Success");
        }
        
    }];
}

// deviceTokenの重複で端末情報の登録に失敗した場合に上書き処理を行う
- (void)updateExistInstallation:(NCMBInstallation*)currentInstallation{
    NCMBQuery *installationQuery = [NCMBInstallation query];
    [installationQuery whereKey:@"deviceToken" equalTo:currentInstallation.deviceToken];
    
    NSError *searchErr = nil;
    NCMBInstallation *searchDevice = [installationQuery getFirstObject:&searchErr];
    
    if (searchErr){
        //端末情報の検索に失敗した場合の処理
        NSLog(@"%@", searchErr);
    } else {
        //上書き保存する
        currentInstallation.objectId = searchDevice.objectId;
        [currentInstallation saveInBackgroundWithBlock:^(NSError *error) {
            if (error){
                //端末情報更新に失敗したときの処理
                NSLog(@"%@", error);
            } else {
                //端末情報更新に成功したときの処理
                NSLog(@"Rewrite Success");
            }
        }];
    }
}

[installation setDeviceTokenFromData:deviceToken];
ここはこれをするだけでいいのは結構嬉しいです!
mBaaS ライブラリのソースをみると < , > , が弾かれるようになっていました。

とりあえずここまででiOSアプリ側のプッシュ通知を受け取るための準備は完了!

mBaaS側作業

Apple Developer Member Center

前提としてすでに上記サイトで AppID を取得してNotificationを設定しているとします。

スクリーンショット_2016-08-02_午後7_40_28.png

スクリーンショット_2016-08-02_午後7_42_38.png

作成した AppID のものを選択する。

スクリーンショット_2016-08-02_午後7_44_00.png

上記画像のようなものがでてくるので Edit をクリック

スクリーンショット_2016-08-02_午後7_45_22.png

まだCertificateを作成していない場合は、KeyChainから証明書を要求しておく

Certificate作成

スクリーンショット 2016-08-02 午後7.47.56.png

スクリーンショット 2016-08-02 午後7.49.00.png

ユーザのメールアドレス はApple Developerに登録しているメールアドレス。通称はなんでもいいのかな?(間違ってたら教えて下さい!!)

適当な場所に作成・保存したら Create Certificate をクリックして指示にしたがって *.cer ファイルをダウンロードする

p12ファイル作成

ダウンロードした aps_development.cer または aps.cer を取得してKeyChainに登録

スクリーンショット_2016-08-02_午後7_54_10.png

こんな感じに追加されるので場合によって p12 ファイルを副クリックから書き出してください。

サーバに適用

ニフティクラウドmobile backend

上記にログインをする。

スクリーンショット_2016-08-02_午後7_58_07.png

65f0dfc5-0d03-e204-860f-0935a2fcb836.png

大体こんな感じに手順をすすめる。

あとは プッシュ通知 タブを選んでそこら辺にある新しくプッシュ通知を作成できそうな以下みたいな画像を押下。
スクリーンショット 2016-08-02 午後8.01.54.png

iOSアプリはすでにプッシュ通知承諾アラートを表示し、OKが選択されてある状態ならば、そのままプッシュ通知を作成する!
あとは待つ!!

これでプッシュ通知が来たらとりあえずは成功!!

プッシュ通知の開封をmBaaSに通知する

AppDelegate.m
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [NCMBAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];
    // 処理
}

プッシュ通知については以前に記事を書いたので以下リンクを参照ください。

アプリが起動されてない場合のPush通知からの起動動作について

おわりに

ざっと簡単な実装をしてみましたが、いかがだったでしょうか?
以前にPythonでプッシュ通知を実装する記事を書いたのですが、こちらも合わせてなんらかのテストができるとちょっといいかもですね!

7
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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?