OGeek|极客世界-中国程序员成长平台

标题: ios - phonegap-plugin-push 不适用于 iOS 9 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:44
标题: ios - phonegap-plugin-push 不适用于 iOS 9

我正在使用新插件“phonegap-plugin-push”,它会覆盖旧的 PushPlugin,以便通过 cordova 应用程序进行推送通知。

通知在 android 和 iOS 8 上都可以正常工作,但是当我使用 iOS 9 时,它注册成功并返回 token ,后端代码返回成功,但设备没有收到通知!

这里是前端代码

var push = PushNotification.init({ "android": {"senderID": "12345679"},
     "ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } );

push.on('registration', function(data) {
    var token = data.registrationId
});

push.on('notification', function(data) {
    // data.message,
    // data.title,
    // data.count,
    // data.sound,
    // data.image,
    var data =  data.additionalData
});

push.on('error', function(e) {
    // e.message
}); 

这是我的后端代码

$deviceToken = "019451814eff224c5dceca49b34b7b635d0716c21da2a77e7fd0809fd508d6z4";
$passphrase = 'myPassPhrase';

$message = 'You have recieved new notification!';


$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$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;

// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'data' => 'test data'
);

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n',         strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

}

请问有什么帮助吗?! 提前致谢



Best Answer-推荐答案


我找到了解决问题的方法,

你可以在这个article找到详细的答案

实际上这不是确定的事情,而是多个因素的组合 1-一定要使用Xcode 7.1.1(目前最新的稳定版本) 2- 一定要创建 APNS Production NOT DEVELOPMENT 证书然后下载它

enter image description here

3- 拖动它或使用钥匙串(keychain)访问打开它展开它并将私钥导出为 yourAppNameKey.p12

enter image description here

4-然后我们需要为证书生成pem文件,所以通过终端写入:

 openssl x509 -in aps_production.cer -inform der -out yourAppNameCert.pem

注意:在最后一步中,我们使用了在步骤 2 中下载的证书

5- 现在我们将私钥的 .p12 文件转换为 .pem 文件:

openssl pkcs12 -nocerts -out yourAppNameKey.pem -in yourAppNameKey.p12 

注意:您将被要求输入用于导出私钥的密码并插入密码短语并确认它以在服务器端代码中使用

6 - 最后,我们将证书和 key 合并到一个 .pem 文件中:

cat PushChatCert.pem PushChatKey.pem > ck.pem

here is a sample of the server side code

希望它适用于每个人......谢谢

关于ios - phonegap-plugin-push 不适用于 iOS 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33730605/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4