简体   繁体   中英

Apple push Notification on sandbox

I've been working on apple push notification. The code works fine for the first time and sends the notification to the device. But it doesn't send for the second time after some interval of first time notification. I don't know why is that. I'm closing the connection.

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ios.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', '1234567881');

// 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) {
    $response['message'] = "Failed to connect: $err $errstr" . PHP_EOL;
    return $response;
}
$response['error'] = false;
$response['message'] = 'Connected to APNS' . PHP_EOL;


// Create the payload body
$body['aps'] = array(
    'alert' => 'this is from test',
    'sound' => 'default'
);

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

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', 'de785a64fd2ee4cc4e0adf067856528c846c0f1e4fe30f88694e59f808f9d3ba') . pack('n', strlen($payload)) . $payload;

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

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;


fclose($fp);

die;

I am using exactly the some code and it's work perfectly. Maybe try to remove the "die" at the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM