我正在构建一个Cordova混合应用程序,它使用推送通知插件 - https://github.com/phonegap/phonegap-plugin-push。对于Android部分,我使用gcm,而对于iOS,我使用APN。一切都适用于Android设备。我可以将APN发送到服务器,它显示一切正常,但我无法在我的iPhone设备上获取它。我可以获得iPhone注册ID并将我的通知直接发送到该设备,但是负责处理通知的功能没有反应。我也尝试重新安装插件,但这没有帮助。
我如何在js中收到通知:
var push = PushNotification.init({
"android": {
"senderID": "xx"
},
"ios": {"alert": "true", "badge": "true", "sound": "true"},
"windows": {}
});
push.on('registration', function(data) {
//I can get registration id here
alert(JSON.stringify(data));
});
push.on('notification', function(data) {
//this place doesn't work
alert("notification event");
alert(JSON.stringify(data));
});
push.on('error', function(e) {
alert("push error");
});
这是我在php中向服务器发送通知的方式:
$deviceToken = 'xxx';
$passphrase = 'xx';
$message = 'My first push notification!';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
fclose($fp);
输出'消息已成功发送'。这有可能在iOS 9上不起作用吗? (我已经在一台设备上进行了测试)也许还有一些其他的想法,为什么它不起作用?