0

我已经制作了一个代码,可以在许多保存在 db 上的设备上发送推送通知(用于分发),我已经制作了 php:

// Put your alert message here:
$message = "send push";
// Put your private key's passphrase here:
$passphrase = 'phrase';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.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'. "<br>" ;
ob_flush();
flush();

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

// Encode the payload as JSON
$payload = json_encode($body);
if (mysql_num_rows($results)!=0) {
    while($row = mysql_fetch_array($results))
    {
        $deviceToken= $row['deviceToken'];

        if(isset($deviceToken)&&(strcmp($deviceToken,"(null)")!=0)){
            echo "<br>".$deviceToken."<br>";
            // Build the binary notification
            $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';
            else
                echo 'Message successfully delivered' ;
            ob_flush();
            flush();
        }
    }
    // Close the connection to the server
    fclose($fp);
}

如果我在 db 上有,只有 deviceToken 正确或具有值(null),则发送消息,但如果我有一个 deviceToken 错误,我会收到消息“消息已成功传递”,该消息已发送所有消息但不起作用。我在哪里犯了错误?谢谢。

4

2 回答 2

0

You does nothing wrong, this is an apple quality control. I met this phenomenon, and - if I remember well - if the structure of the device token was invalid, no push notification was delivered...

So, if you have a valid token, which currently does not belong to a device (e.g the device has removed your app), your other push notification will be delivered, but if a token seems to have an absolutely incorrect structure, no push notification will be delivered.

So, this phenomenon will not be dangerous for your production system, because your device will register a valid token to your provider's DB.

Maybe more details are between the comment of this tutorial: http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12

于 2012-09-06T20:46:25.050 回答
0

这是无法做到的。没有可靠的方法来判断推送通知是否成功发送。有替代品,但它们不是即时的,也不那么可靠。

  1. 确保应用程序安装在用户设备上。
  2. 将逻辑添加到您的应用程序以在收到推送通知时发布到您的数据库。这样你就知道推动是好的。这样做的缺点是推送通知并不总是即时的,因此可能会有延迟。

一位用户对此进行了解释,并在另一篇文章中给出了一些链接。如何从我们的服务器中找到发送给用户的 Apple 推送通知?

于 2012-09-06T22:44:30.153 回答