菜鸟教程小白 发表于 2022-12-12 12:41:06

php - APNS PHP 不发送通知


                                            <p><p>我已经使用 PHP 设置了 APNS Provider Server。我没有错误地连接到 Apple 的 APNS 服务器,并且脚本似乎运行良好,循环通过所有设备并发送有效负载。但是,订阅通知的设备不会收到通知。有人可以帮我找出代码中的错误吗?</p>

<pre><code>$message = $_POST[&#39;message&#39;];
echo &#34;Posted Message: $message&lt;br /&gt;&#34;;

$message = str_replace(&#34;&#39;&#34;, &#34;\&#39;&#34;, $message);
echo &#34;Formatted message: $message&lt;br /&gt;&#34;;

$title = $_POST[&#39;title&#39;];
$title = str_replace(&#34;&#39;&#34;, &#34;\&#39;&#34;, $title);
echo &#34;Formatted Title: $title&lt;br /&gt;&#34;;

$category = $_POST[&#39;category&#39;];
echo &#34;Category: $category&lt;br /&gt;&#34;;

$alert = array(&#39;title&#39; =&gt; &#34;$title&#34;, &#39;body&#39; =&gt; &#34;$message&#34;);
foreach ($alert as $key =&gt; $value){
    echo &#34;Key: $key Value: $value&lt;br /&gt;&#34;;
}

ini_set(&#34;display_errors&#34;,1);

$apnsHost = &#39;gateway.push.apple.com&#39;;
$apnsPort = 2195;
$apnsCert = &#39;apns.pem&#39;;

$streamContext = stream_context_create();
stream_context_set_option($streamContext, &#39;ssl&#39;, &#39;local_cert&#39;, $apnsCert);

$apns = stream_socket_client(&#39;ssl://&#39; . $apnsHost . &#39;:&#39; . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
if ($error!=&#34;&#34; || $errorString!=&#34;&#34;){
    echo &#34;Error Connecting: $error : $errorString&lt;br /&gt;&#34;;
}
else{
    echo &#34;No Error Connecting&lt;br /&gt;&#34;;
}

$query = &#34;INSERT INTO notifications (`title`, `message`, `category`) VALUES (\&#34;$title\&#34;, \&#34;$message\&#34;, \&#34;$category\&#34;)&#34;;
echo &#34;Insert Query: $query&lt;br /&gt;&#34;;

$result1 = mysqli_query($connection, $query);

$query = &#34;SELECT * FROM notifications ORDER BY notification_id DESC&#34;;
$result931 = mysqli_query($connection, $query);
$row = $result931-&gt;fetch_assoc();
$notification_id = $row;

$query = &#34;SELECT * FROM category_subscriptions WHERE category_id=&#39;$category&#39; ORDER BY device_id&#34;;
$result1 = mysqli_query($connection, $query);
echo &#34;Subscriber Query: $query&lt;br /&gt;&#34;;
while($row = $result1-&gt;fetch_assoc()) {
    $device_id = $row;
    $query = &#34;SELECT * FROM devices WHERE device_id=&#39;$device_id&#39; ORDER BY device_id&#34;;
    $result = mysqli_query($connection,$query);

    while($r = $result-&gt;fetch_assoc()) {
      $deviceToken = $r;
      $badge = $r + 1;
      if ($r==1) {
            if ($r==1) {
                if ($r==1) {
                  $payload[&#39;aps&#39;] = array(&#39;alert&#39; =&gt; $alert, &#39;badge&#39; =&gt; $badge, &#39;sound&#39; =&gt; &#39;default&#39;);
                } else {
                  $payload[&#39;aps&#39;] = array(&#39;alert&#39; =&gt; $alert, &#39;badge&#39; =&gt; $badge);
                }
            }
            else if ($r==1) {
                  $payload[&#39;aps&#39;] = array(&#39;alert&#39; =&gt; $alert, &#39;sound&#39; =&gt; &#39;default&#39;);
            }
            else {
                $payload[&#39;aps&#39;] = array(&#39;alert&#39; =&gt; $alert);
            }
            $payload1 = json_encode($payload);
            echo &#34;Payload: $payload1&lt;br /&gt;&#34;;

            echo &#34;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Sending to Device Token: $deviceToken&lt;br /&gt;&#34;;
            $apnsMessage = chr(0) . chr(0) . chr(32) . pack(&#39;H*&#39;, str_replace(&#39; &#39;, &#39;&#39;, $deviceToken)) . chr(0) . chr(strlen($payload1)) . $payload1;
            echo &#34;APNS Message: $apnsMessage&lt;br /&gt;&#34;;
            fwrite($apns, $apnsMessage);
            $query6 = &#34;UPDATE devices SET badge_value = $badge WHERE device_id=&#39;$device_id&#39;&#34;;
            $result6 = mysqli_query($connection,$query6);
            $query7 = &#34;INSERT INTO notification_map (`device_id`, `notification_id`, `viewed`) VALUES ($device_id, $notification_id, \&#34;false\&#34;)&#34;;
            $result7 = mysqli_query($connection,$query7);
      }
    }
}
fclose($apns);
</code></pre>

<p>截图:
<a href="/image/zQljA.png" rel="noreferrer noopener nofollow"><img src="/image/zQljA.png" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>执行以下步骤:<br/>
1.检查pem文件是否有效<br/>
2. 检查你的服务器是否打开了2195端口。</p>

<h2> 3. 如果您的 pem 文件是开发模式,则使用“ssl://gateway.sandbox.push.apple.com:2195”连接到 apns,否则如果您的 pem 文件是生产模式,则使用“ssl://gateway.push.apple.com:2195".</h2>

<ol start="4">
<li>检查密码是否正确。</li>
</ol>

<p>检查此示例代码:</p>

<pre><code>&lt;?php

// Put your device token here (without spaces):
$deviceToken = &#39;fefb03ba6adcea310cf3f455dae16fec4f63b4ba4d96103c20d594a04efd7c2a&#39;;
// Put your private key&#39;s passphrase here:
$passphrase = &#39;Welcome@1&#39;;

// Put your alert message here:
$message = &#39;My first push 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;title&#39; =&gt; &#39;testing title&#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 style="font-size: 20px;">关于php - APNS PHP 不发送通知,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41303313/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41303313/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: php - APNS PHP 不发送通知