APNS Bulk不提供但单独工作

时间:2016-02-16 22:11:22

标签: php ios apple-push-notifications

从samplePush.php构建我已经能够为我的应用程序构建一个事务性APNS脚本。这很好用,让我能够一对一推送电话通知。

我需要将其移至批处理通知并构建:

$APNSMessage = 'Test from APNS Bulk!';

// Bulk PUSH is the service designed to send iOS PUSH Notifications to hundreds of devices all at the same time using the same APNS connection. 
function bulkPushToAPNS($TokenArray, $MessageToPush) {
echo "Sending to " . count($TokenArray) . " Devices ";

 $body = array('aps' => array('alert' => $MessageToPush, 'badge' => 1, 'sound' => 'default'));
        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', 'apnsCert.pem');
        $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
        if (!$fp) {
            print "Failed to connect $token $err $errstrn";
            return;
        }
        print "Connection OK ";
        $payload = json_encode($body);
        foreach($TokenArray as $token) {
            $msg = chr(0) . chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $token)) . pack("n",strlen($payload)) . $payload;
            print $payload;
            //fwrite($fp, $msg);
            $result = fwrite($fp, $msg, strlen($msg));

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


        }
        fclose($fp);

}
$TokensToPushToArray = array('ThisIsTokenOne', 'ThisIsTokenTwo');
bulkPushToAPNS($TokensToPushToArray, $APNSMessage);

当脚本运行时,它会传递一个需要此消息的iOS令牌数组。 脚本的输出是:

Sending to 2 Devices Connection OK {"aps":{"alert":"Test from APNS Bulk!","badge":1,"sound":"default"}}Message successfully delivered {"aps":{"alert":"Test from APNS Bulk!","badge":1,"sound":"default"}}Message successfully delivered

所以来自Apple的反馈意见是这些正在传递。但没有任何东西可以用于手持设备。

如果我单独运行每个设备令牌它们都可以正常工作,那么批量播放中的某些东西会起作用吗?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在这里回答我自己的问题; 移动代码,使foreach在连接内:

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apnsCert.pem');

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

foreach ($APNSTokens as $APNSToken) {

// Create the payload body
$body['aps'] = array(
    'alert' => $APNSMessage,
    'sound' => 'default',
    'badge' => 1
    );

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

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $APNSToken) . 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;

}

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

现在连接仍然存在并且一直运行直到foreach完成。 经过测试和工作。