4

I have a list of push notification tokens (around 10k) for my app, and I am not tracking the states of active/deleted tokens. When a new user is added to the system, it is added with its push token

(one device can hold one push notification token, in case a token is changed -not happened in the last 6 months of my app's life cycle- it is modified with the new token)

For sending push notification to a single user I am using the following php script

<?php
// connecting to sql
sql_connect();
// get user token from sql
$token = sql_gettoken();

// Put your device token here (without spaces):
$deviceToken = $token;
// Put your private key's passphrase here:
$passphrase = 'my_passphrase';

// Put your alert message here:
if ($_GET["message"]) $message = $_GET["message"];
else $message = 'This is a default message in case no message is defined';

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

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'production_cerfiticate.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'
    );

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

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

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

I believe if I keep $fp (connection to APNS Server) open I can send multiple push notifications in between (I am not sure of that?). But I have read that if a token is offline/deleted app then the connection is closed by apple's side, which means push notification cycle will fail.

Any tips for converting this single push notification script into broadcast notification ? I am discouraged to test this since the first test will be a live broadcast and I have only 1 shot at this.

EDIT: I have missed the most important part, If I just go and call this php script in a loop for all device tokens, which means it will try to call this for deleted apps and actives, will Apple block/deactivate/ban my certificate or my ability to send push notifications ?

  • For others out there looking for broadcast, it seems Apple does not have any limits for push notifications.

Cheers

4

1 回答 1

3

只要连接保持打开状态,您就应该保持连接打开并使用相同的连接发送通知。这就是苹果说你应该做的。

如果从设备上卸载应用程序,发送到该设备时连接不会关闭。仅当令牌对当前环境从未有效(这通常发生在您尝试将沙盒设备令牌发送到生产环境时发生,反之亦然)或消息有其他错误(例如有效负载过长)时才会关闭它)。

当您向已卸载应用程序的设备发送通知时,APNS 会发现该设备已卸载该应用程序,然后,当您调用反馈服务时,您会获得该设备令牌。Apple 要求您定期调用反馈服务并从您的数据库中删除它返回的设备。如果您不使用反馈服务并继续向多次卸载该应用程序的设备发送通知,Apple 可能会阻止您。

我建议您阅读有关故障排除推送通知的内容,尤其是推送通知吞吐量和错误检查部分。这很有帮助。

于 2013-04-13T04:09:12.207 回答