菜鸟教程小白 发表于 2022-12-12 09:44:12

ios - phonegap-plugin-push 不适用于 iOS 9


                                            <p><p>我正在使用新插件“<a href="https://github.com/phonegap/phonegap-plugin-push" rel="noreferrer noopener nofollow">phonegap-plugin-push</a>”,它会覆盖旧的 <a href="https://github.com/phonegap-build/PushPlugin" rel="noreferrer noopener nofollow">PushPlugin</a>,以便通过 cordova 应用程序进行推送通知。</p>

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

<p>这里是前端代码</p>

<pre><code>var push = PushNotification.init({ &#34;android&#34;: {&#34;senderID&#34;: &#34;12345679&#34;},
   &#34;ios&#34;: {&#34;alert&#34;: &#34;true&#34;, &#34;badge&#34;: &#34;true&#34;, &#34;sound&#34;: &#34;true&#34;}, &#34;windows&#34;: {} } );

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

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

push.on(&#39;error&#39;, function(e) {
    // e.message
});
</code></pre>

<p>这是我的后端代码</p>

<pre><code>$deviceToken = &#34;019451814eff224c5dceca49b34b7b635d0716c21da2a77e7fd0809fd508d6z4&#34;;
$passphrase = &#39;myPassPhrase&#39;;

$message = &#39;You have recieved new notification!&#39;;


$ctx = stream_context_create();
stream_context_set_option($ctx, &#39;ssl&#39;, &#39;local_cert&#39;, &#39;ck.pem&#39;);
stream_context_set_option($ctx, &#39;ssl&#39;, &#39;passphrase&#39;, $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
&#39;ssl://gateway.sandbox.push.apple.com:2195&#39;, $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
exit(&#34;Failed to connect: $err $errstr&#34; . PHP_EOL);

echo &#39;Connected to APNS&#39; . PHP_EOL;

// Create the payload body
$body[&#39;aps&#39;] = array(
&#39;alert&#39; =&gt; $message,
&#39;sound&#39; =&gt; &#39;default&#39;,
&#39;data&#39; =&gt; &#39;test data&#39;
);

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

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

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

if (!$result)
echo &#39;Message not delivered&#39; . PHP_EOL;
else
echo &#39;Message successfully delivered&#39; . PHP_EOL;

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

}
</code></pre>

<p>请问有什么帮助吗?!
提前致谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我找到了解决问题的方法,</p>

<p>你可以在这个<a href="http://w34.co/tutorials/creating-pem-file-for-ios-push-notifications/" rel="noreferrer noopener nofollow">article</a>找到详细的答案</p>

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

<p> <a href="/image/cl8sJ.png" rel="noreferrer noopener nofollow"><img src="/image/cl8sJ.png" alt="enter image description here"/></a> </p>

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

<p> <a href="/image/EH0Jp.png" rel="noreferrer noopener nofollow"><img src="/image/EH0Jp.png" alt="enter image description here"/></a> </p>

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

<pre><code> openssl x509 -in aps_production.cer -inform der -out yourAppNameCert.pem
</code></pre>

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

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

<pre><code>openssl pkcs12 -nocerts -out yourAppNameKey.pem -in yourAppNameKey.p12
</code></pre>

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

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

<pre><code>cat PushChatCert.pem PushChatKey.pem &gt; ck.pem
</code></pre>

<p> <a href="https://drive.google.com/file/d/0B0zVybqvfI8dY0JwYW1mWENEcWc/view?usp=sharing" rel="noreferrer noopener nofollow">here is a sample of the server side code</a> </p>

<p>希望它适用于每个人......谢谢</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - phonegap-plugin-push 不适用于 iOS 9,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/33730605/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/33730605/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - phonegap-plugin-push 不适用于 iOS 9