Objective c 向所有用户发送推送通知
所以,我有一个应用程序。此应用程序使用以下PHP代码发送推送通知:Objective c 向所有用户发送推送通知,objective-c,ios,push-notification,apple-push-notifications,Objective C,Ios,Push Notification,Apple Push Notifications,所以,我有一个应用程序。此应用程序使用以下PHP代码发送推送通知: <?php $deviceToken = '4bc9b8e71b9......235095a22d'; // Put your private key's passphrase here: $passphrase = '12345'; // Put your alert message here: $message = 'My Message Here!'; ////////////////////////////
<?php
$deviceToken = '4bc9b8e71b9......235095a22d';
// Put your private key's passphrase here:
$passphrase = '12345';
// Put your alert message here:
$message = 'My Message Here!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// 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;
// 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);
您必须循环所有设备令牌并使用fwrite()将它们写入网关您必须循环所有设备令牌并使用fwrite()将它们写入网关通常的方法是将令牌存储在数据库中,一旦需要发送它们,只需从数据库中选择令牌并循环使用即可
代码可能是这样的
$pdo = new PDO(
"mysql:host=$db_host;port=$db_port;dbname=$db_name",
$db_user,
$db_pass
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$select_tokens_sql = 'SELECT * FROM tokens';
$select_tokens_statement = $pdo->prepare($select_tokens_sql);
// 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;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
$select_tokens_statement->execute();
$tokens = $select_tokens_statement->fetchAll();
//loop through the tokens
foreach($tokens as $token) {
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $token) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message to the device ' . $token . ' not delivered' . PHP_EOL;
else
echo 'Message to the device ' . $token . ' successfully delivered' . PHP_EOL;
}
// Close the connection to the server
fclose($fp);
在发送完推送通知后,听听苹果的反馈服务可能也是个好主意。它会告诉您,在某些设备上,您的应用程序是否不再存在,以便您可以安全地从数据库中删除相应的令牌。通常的方法是将令牌存储在数据库中,一旦您需要发送它们,只需从数据库中选择令牌并循环它们即可
代码可能是这样的
$pdo = new PDO(
"mysql:host=$db_host;port=$db_port;dbname=$db_name",
$db_user,
$db_pass
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$select_tokens_sql = 'SELECT * FROM tokens';
$select_tokens_statement = $pdo->prepare($select_tokens_sql);
// 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;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
$select_tokens_statement->execute();
$tokens = $select_tokens_statement->fetchAll();
//loop through the tokens
foreach($tokens as $token) {
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $token) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message to the device ' . $token . ' not delivered' . PHP_EOL;
else
echo 'Message to the device ' . $token . ' successfully delivered' . PHP_EOL;
}
// Close the connection to the server
fclose($fp);
在发送完推送通知后,听听苹果的反馈服务可能也是个好主意。它将告诉您,在某些设备上,您的应用程序是否不再存在,以便您可以安全地从数据库中删除相应的令牌。您需要让您的应用程序将其返回的APNs设备令牌发送回服务器。对不起,这是我第一次使用推送通知。你能解释清楚吗?谢谢你知道一些我想要的教程吗?你需要让你的应用程序将它得到的APNs设备令牌发送回你的服务器。对不起,这是我第一次使用推送通知。你能解释清楚吗?谢谢你知道我想要什么的一些教程吗?所以,我需要在数据库中添加每个设备令牌,在php代码中,我将做一个循环,将消息发送到每个设备?是的,这就是你应该做的。所以,我需要在数据库中添加每个设备令牌,在php代码中,我将做一个循环,将消息发送到每个设备?是的,这就是你应该做的。如果我必须向1000万台设备发送推送通知,这是合适的代码吗?如果我必须向1000万台设备发送推送通知,这是合适的代码吗?