0

我在我的 PHP 项目中使用 Apple 推送通知。当我使用静态设备令牌(静态字符串)向多个设备发送通知时它工作正常但是当我从数据库中获取它时它不会发送通知但我显示消息的成功传递。当我比较来自数据库的值和我的静态值时,它们是相同的。请帮我。

谢谢

这是我的代码片段

while($resultRow = mysql_fetch_object($subscriberResult))
{


    if($resultRow->device_type =='android')
    {
        $gcm = new GCM();

        $registatoin_ids = array($resultRow->device_id);
        $message = array("message" => $msgText);

        $result = $gcm->send_notification($registatoin_ids, $message);//send notification on andoroid
    }
    elseif($resultRow->device_type =='ios')
    {
    // Put your device token here (without spaces):
        $deviceToken =  $resultRow->device_id;

    //$deviceToken = 'eb4085335d91d0ae07a2736dd0fc0ccb095486a1d54bf9addc75785f427fea91';
    // Put your private key's passphrase here:
        $passphrase = 'pushchat';

    // Put your alert message here:
        $message = $msgText;

    ////////////////////////////////////////////////////////////////////////////////

        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', 'server_certificates_bundle_sandbox.pem');
        stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
        stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer');

    // 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'
        );

    // 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));
        echo $result."</br>";
        if (!$result)
            echo 'Message not delivered' . PHP_EOL;
        else
            echo 'Message successfully delivered' . PHP_EOL;
    }
}
// Close the connection to the server if $fp defined
if($fp) {
    fclose($fp);
}
4

0 回答 0