LoginSignup
6
7

More than 5 years have passed since last update.

Parse.comでPUSH通知(PhoneGap Build,iOS)メモ

Last updated at Posted at 2015-10-21

はじめに

以下の続き的メモ。
PhoneGap Buildでionicを使う準備メモ
http://qiita.com/xylitol45@github/items/1b296ef3fc9b3d43f877

mBaaSとして、Parse.com( https://www.parse.com/ )を利用し、PUSH通知ができるようにする。
手元にiPhoneしかないので、iOSで試す。

準備

このへんを見ながらPUSH通知用のAppIDやら証明書を準備。

iOSでプッシュ通知を実装する方法の超詳細まとめ
http://www.lancork.net/2013/08/how-to-ios-push-first/
http://www.lancork.net/2013/08/how-to-ios-push-second/

ちなみにAWS SNSの場合、CertificateSigningRequest.certSigningRequestの通称の日本語はNGのようなのだが、Parse.comは大丈夫だった。

Push通知がコマンドラインからは送れるのにAWS SNSにp12ファイルが設定出来ない場合
http://qiita.com/soramugi/items/3fdce462e4b28a5022da

PhoneGap Build

署名キー

署名書キーを登録。

  • 開発用証明書を書き出したp12ファイル(パスワードあり)
  • Push Notificationsと紐付けられているプロビジョニングプロファイル

PushPlugin

サードパーティプラグインのPushPluginを利用する。
PhoneGap Build の PushPlugin
https://build.phonegap.com/plugins/3960

config.xmlに以下を追加する。

config.xml
    <!-- pushplugin追加 -->
    <gap:plugin name="com.phonegap.plugins.pushplugin" />
    <!-- アクセス許可サイト -->    
    <access origin="*"/>

Parse.com

このへんを見ながら。
Parse.comを使ったiOSデバイスへのプッシュ通知[導入編]
http://www.riaxdnp.jp/?p=6650

Settings => Push => Apple Push Certificates で、PUSH通知証明書をp12ファイル(パスワードなし)で書き出したものを登録。

JavaScript修正

このへんを見ながらapp.jsを修正する。
phonegap-build/PushPlugin
https://github.com/phonegap-build/PushPlugin/blob/a172e19/README.md

アプリ起動時にrunメソッドが実行され、Parse.comにトークンを登録する。

app.js
"use strict";

//PUSH通知受信
var onMyNotificationAPN = function(event) {
    if ( event.alert ) {
        alert("my notification " + event.alert);
    }
};

angular.module('myApp', ['ionic','ngCordova','myApp.controllers'])

.factory('shared',['$http','$cordovaDevice',function($http,$cordovaDevice){
    var _o = {
        // トークン取得、Parse.com登録
        notifyRegisetr:function(){
            if ($cordovaDevice.getPlatform() == 'iOS') {
                _o.notifyRegisterForiOS();
            }
        },
        // for iOS
        notifyRegisterForiOS: function() {
            var pushNotification = window.plugins.pushNotification;
            //alert('notifyRegisterForiOS');
            pushNotification.register(
                function(token){
                    console.log("token:"+token);
                    // REST APIを利用し、Parse.com へトークン登録
                    $http({
                        'method':'POST',
                        'url':'https://api.parse.com/1/installations',
                        'headers':{
                            "X-Parse-Application-Id":"[Application Id]",
                            "X-Parse-REST-API-Key": "[REST API Key]" 
                        },
                        'data':{
                            "deviceType": "ios",
                            "deviceToken": token,
                            "channels": [""]
                          }
                    });
                },
                function(err){
                    // トークン取得失敗
                    // console.log("error:"+err);
                },
                {
                    "badge":"true",
                    "sound":"true",
                    "alert":"true",
                    "ecb": "onMyNotificationAPN"
                }
            );
        }
    };
    return _o;
}])

.run(['shared','$cordovaStatusbar',function(shared,$cordovaStatusbar) { 
    ionic.Platform.ready(function(){
        $cordovaStatusbar.overlaysWebView(true);

        shared.notifyRegisterForiOS();
     });
}])
;

Parse.comでPUSH通知

Push => Send a push で登録済み端末へPUSH通知を送る。
なお、登録端末は Core => Data => Installation で確認可能。

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