Apple Wallet推送通知从PHP发送问题

时间:2017-06-23 20:03:29

标签: php apple-push-notifications wallet passkit

我检查了大多数帖子尝试做的所有事情,但仍然无法在我的手机上接收推送通知。

问题是没有错误显示“消息成功传递字符串(141)”。在手机上我没有收到任何东西。请你看看代码&点区域,我必须看到错误。

<?php
require_once __DIR__.'/../mobilewallet/config.php';
$errstrn = "";

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'passphrase', $push_cert_passphrase);
stream_context_set_option($ctx, 'ssl', 'local_cert', $push_cert_uri_pem);
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
stream_set_blocking ($fp, 0); 


if (!$fp) {
    //ERROR
    echo "Failed to connect (stream_socket_client): $err $errstrn";
} else {

    // Create the payload body

    $body['aps'] = array(
      'alert' => "Hello World",
      'sound' => 'default',
      'link_url' => "http://www.abc.en",
    );

    //$payload = json_encode($body);
    $payload = json_encode($body);
    //Enhanced Notification
    $msg = chr(0) . pack('n', 32) . pack('H*', $push_device_token) . pack('n', strlen($payload)) . $payload;

    //SEND PUSH
    $result = fwrite($fp, $msg); 
    //We can check if an error has been returned while we are sending, but we also need to check once more after we are done sending in case there was a delay with error response.
    checkAppleErrorResponse($fp);
    checkAppleErrorResponse($fp);
    usleep(500000); //Pause for half a second. Note I tested this with up to a 5 minute pause, and the error message was still available to be retrieved
    checkAppleErrorResponse($fp);

    if (!$result)
      echo 'Message not delivered' . PHP_EOL;
    else {
      echo 'Message successfully delivered ' . PHP_EOL;
      var_dump('message ' .$msg .'result   {' .$result .'}');
    }

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


function checkAppleErrorResponse($fp) {

   //byte1=always 8, byte2=StatusCode, bytes3,4,5,6=identifier(rowID). Should return nothing if OK.
   $apple_error_response = fread($fp, 6);
   //NOTE: Make sure you set stream_set_blocking($fp, 0) or else fread will pause your script and wait forever when there is no response to be sent.
   if ($apple_error_response) {
        //unpack the error response (first byte 'command" should always be 8)
        $error_response = unpack('Ccommand/Cstatus_code/Nidentifier', $apple_error_response);

        if ($error_response['status_code'] == '0') {
            $error_response['status_code'] = '0-No errors encountered';
        } else if ($error_response['status_code'] == '1') {
            $error_response['status_code'] = '1-Processing error';
        } else if ($error_response['status_code'] == '2') {
            $error_response['status_code'] = '2-Missing device token';
        } else if ($error_response['status_code'] == '3') {
            $error_response['status_code'] = '3-Missing topic';
        } else if ($error_response['status_code'] == '4') {
            $error_response['status_code'] = '4-Missing payload';
        } else if ($error_response['status_code'] == '5') {
            $error_response['status_code'] = '5-Invalid token size';
        } else if ($error_response['status_code'] == '6') {
            $error_response['status_code'] = '6-Invalid topic size';
        } else if ($error_response['status_code'] == '7') {
            $error_response['status_code'] = '7-Invalid payload size';
        } else if ($error_response['status_code'] == '8') {
            $error_response['status_code'] = '8-Invalid token';
        } else if ($error_response['status_code'] == '255') {
            $error_response['status_code'] = '255-None (unknown)';
        } else {
            $error_response['status_code'] = $error_response['status_code'] . '-Not listed';
        }

        echo '<br><b>+ + + + + + ERROR</b> Response Command:<b>' . $error_response['command'] . '</b>&nbsp;&nbsp;&nbsp;Identifier:<b>' . $error_response['identifier'] . '</b>&nbsp;&nbsp;&nbsp;Status:<b>' . $error_response['status_code'] . '</b><br>';
        echo 'Identifier is the rowID (index) in the database that caused the problem, and Apple will disconnect you from server. To continue sending Push Notifications, just start at the next rowID after this Identifier.<br>';

        return true;
   }
   return false;
}

0 个答案:

没有答案