我试图从 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
- 所以因为我的事件是“设备就绪”,所以 app.recievedEvent 应该开始,对吧?
- 并且因为我的设备是 IOs-设备 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);