在iphone上推送通知

时间:2014-05-23 14:41:07

标签: php iphone

我无法使用下面提到的代码在苹果设备上获得推送通知

function sendNotificationToiPhone($iUSERId, $strMessage, $pushType, $userWhoIsFllowing, $type, $typId)
{
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', $_SERVER['DOCUMENT_ROOT'] . '/qa/api/ck.pem');
    // assume the private key passphase was removed.

    stream_context_set_option($ctx, 'ssl', 'demoname', 'anypsw');
    // ssl://gateway.push.apple.com:2195 //if sending it to app store build

    $fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    if (! empty($fp)) 

    {
        $arrBody['aps'] = array(
            'alert' => 'hello',
            'sound' => 'default'
        );

        // genrating payload
        $payload = json_encode($arrBody);
        echo $payload . "<br>";
        $deviceToken = "cc256a032af46d156a403ebc4a693b537b83bf14131bbb993028d1649b5128b8";
        $msg = chr(0) . pack("n", 32) . pack('H*', $deviceToken) . pack("n", strlen($payload)) . $payload;

        echo fwrite($fp, $msg);
    } else {
        echo "connection failed in iphone";
    }
    fclose($fp); // closing notification connection
}

该代码给出了回复 -

{&#34; APS&#34; {&#34;警报&#34;:&#34;你好&#34;&#34;声音&#34;:&#34;默认&#34;} } 80

提前致谢

1 个答案:

答案 0 :(得分:0)

试试这个:

<?php


$deviceToken = '556666..................';


$passphrase = '';


$message = 'Recieved Notification';

$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' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
        'alert' => $message,
    'sound' => 'default',
    'content-available' => '1'
    );

// 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));
print_r($result);
if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;


fclose($fp);