Holly Schinsky http://devgirl.org/2012/10/19/tutorial-apple-push-notifications-with-phonegap-part-1/からこのチュートリアルを実行しようとしました
プラグインがインストールされ、最初のプッシュを取得しようとしています
私のindex.htmlで、src index.jsから関数 app.initialize() を呼び出します
これは私のindex.js内にあります:
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
tokenHandler:function(msg) {
console.log("Token Handler " + msg);
},
errorHandler:function(error) {
console.log("Error Handler " + error);
alert(error);
},
successHandler: function(result) {
alert('Success! Result = '+result)
},
receivedEvent: function(id) {
var pushNotification = window.plugins.pushNotification;
// TODO: Enter your own GCM Sender ID in the register call for Android
if (device.platform == 'android' || device.platform == 'Android') {
pushNotification.register(this.successHandler, this.errorHandler, {"senderID":"554205989074","ecb":"app.onNotificationGCM"});
}
else {
pushNotification.register(this.tokenHandler,this.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
}
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
},
// iOS
onNotificationAPN: function(event) {
var pushNotification = window.plugins.pushNotification;
console.log("Received a notification! " + event.alert);
console.log("event sound " + event.sound);
console.log("event badge " + event.badge);
console.log("event " + event);
if (event.alert) {
navigator.notification.alert(event.alert);
}
if (event.badge) {
console.log("Set badge on " + pushNotification);
pushNotification.setApplicationIconBadgeNumber(this.successHandler, event.badge);
}
if (event.sound) {
var snd = new Media(event.sound);
snd.play();
}
},
// Android
onNotificationGCM: function(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
alert('registration id = '+e.regid);
}
break;
case 'message':
// this is the actual push notification. its format depends on the data model
// of the intermediary push server which must also be reflected in GCMIntentService.java
alert('message = '+e.message+' msgcnt = '+e.msgcnt);
break;
case 'error':
alert('GCM error = '+e.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
}
};
コードが多すぎる場合は申し訳ありませんが、どこから始めればよいか正確にはわかりません。私はこれにかなり慣れていませんが、何が起こったのかを説明しようとします:
- 関数 app.initialize を呼び出します
- XCode での出力は次のとおりです。 [LOG] Received Event: deviceready
- 私のイベントは「deviceready」なので、 app.recievedEvent が開始するはずですよね?
- 私のデバイスは IOs-Device であるため、pushNotification.register を開始する必要があります。
--> しかし、何も起こりません (Xcode で出力を取得することに加えて: [LOG] Received Event: deviceready)
デバイスでプッシュ通知を受信しません
ここに私のphpファイルからのコード:
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', 'cert.pem');
$socketClient = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
$payload['aps'] = array('alert' => 'test message', 'sound' => 'default', 'badge' => '2');
$payload['id'] = 666;
$payload = json_encode($payload);
$deviceToken = str_replace(' ', '', $data['deviceToken']);
$message = pack('CnH*', 0, 32, $deviceToken);
$message = $message . pack('n', strlen($payload));
$message = $message . $payload;
fwrite($socketClient, $message);
fclose($socketClient);