How to know all devices tokens to send push notification (APNS) in a loop?

半腔热情 提交于 2019-12-20 15:24:51

问题


I'm trying to use a php code to send to all tokens who downloaded my iPhone application. Could you tell me how to send to multiple devices and how to get into a loop of devices tokens?

this is my code:

<?php

$deviceToken = ''; // HERE I CAN SEND TO ONE DEVICE

// Passphrase for the private key (ck.pem file)
// $pass = '';
// Get the parameters from http get or from command line
$message = $_GET['message'] or $message = $argv[1] or $message = 'MY NOTIFICATION BODY';
$badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
$sound = $_GET['sound'] or $sound = $argv[3];

// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);

if ($badge)
    $body['aps']['badge'] = $badge;
if ($sound)
    $body['aps']['sound'] = $sound;
/* End of Configurable Items */

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-dev.pem');
// assume the private key passphase was removed.
// stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60,STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
print "Failed to connect $err $errstrn";
return;
}
else {
print "Connection OK\n";
}

$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) .      pack("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);

?>

回答1:


I wrote a tutorial on push notifications. I suggest you read it so you will better understand what you're supposed to do: http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12




回答2:


Push messages have to be sent one by one. You'll have to use a foreach loop to loop through the device tokens:

Here's a simple example:

foreach ( $device_tokens as $device_token )
{
  // Send device token a message here.
}

Where $device_tokens is an array of device tokens.



来源:https://stackoverflow.com/questions/6391351/how-to-know-all-devices-tokens-to-send-push-notification-apns-in-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!