I can not send push messages to multiple iOS devices querying the database, it only sends it to the last record , I tried various solutions found in stackoverflow but it still fails in any , can anyone help me?
<form action="" method="post">
<label for="txtmensagem">Mensagem:</label>
<input type="text" name="txtmensagem" size="30"/>
<input type="submit" value="Enviar"/>
</form>
<?php
//conect with database
include_once('conexao.php');
$message = stripslashes(ucfirst($_POST['txtmensagem']));
// executa a query
$dados = mysql_query("SELECT * FROM token_push WHERE so='ios'");
$passphrase = 'XXXXX'; //pushchat
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'xx.pem');//xx.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 conect to APNS
//if (!$fp)
//exit("Falha na conexão: $err $errstr" . PHP_EOL);
//echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
if (!empty($message)){
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'conteudo' => $conteudo,
'comando' => 'noticia',
'badge' => +1,
'idConteudo' => mysql_insert_id(),
);
// Encode the payload as JSON
$payload = json_encode($body);
//insert token in array
while ($row = mysql_fetch_array($dados)) {
$deviceToken = array();
$deviceToken[] = $row['token'];
}
foreach($deviceToken as $device){
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $device) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
}
}
//return if send or not
if (!$result)
echo 'Ops!' . PHP_EOL;
else
echo 'Aehoooooo!' . PHP_EOL;
// Close the connection to the server
fclose($fp);
Related
I try to send multiple notifications with one hit. Many users have more than one device and i put their device tokens into an array. It is working fine but if there is a wrong token Apple is disconnecting me and the device with working token gets no push notification. I've read about a error-response-solution at this post: Apple Push Notification Limits
But this solution is not working for me. It says: Status:7-Invalid payload size.
But there is only the message "test" and I'm sure the reason is the wrong token. How could I continue the foreach script without disconnecting?
// Put your device token here (without spaces):
$deviceToken = array('WRONG_TOKEN', 'WORKING_TOKEN');
// Put your private key's passphrase here:
$passphrase = 'PASS';
// Put your alert message here:
$message = "test";
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'cer.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' => "www/sound.mp3",
'content-available' => 1,
'badge' => 1
);
// Encode the payload as JSON
$payload = json_encode($body);
//foreach für array
foreach($deviceToken as $token){
$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));
}//foreach end
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
//checkAppleErrorResponse($fp);
// Close the connection to the server
fclose($fp);
This is my php push notification code for ios below:
<?php
// Put your device token here (without spaces):
$deviceToken = "********************************************";
// Put your private key's passphrase here:
$passphrase = "********";
function pushIOSNotification($message)
{
GLOBAL $deviceToken;
GLOBAL $passphrase;
$ctx = stream_context_create();
$path = dirname(__FILE__).'/ck.pem';
//echo $path;
stream_context_set_option($ctx, 'ssl', 'local_cert', $path);
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);
}
This code is only for one device,
if I need send to everyone and support localization,
this code will be like these below.
foreach($deviceTokens as $deviceToken){
$deviceOS = "query this device os from memcache";
if($deviceOS == "ios")
{
$lang = "query this device langauge from memcache";
$message = $messageData[$lang];
pushIOSNotification($deviceToken,$message);
}
}
but I doubt this performance of this code,
because deviceTokens length is the number of all members.
I followed Ray Wenderlich's tutorial for remote notifications on iOS, and had no issues with development. In production, however, my notifications are failing with the error: Failed to connect: 0
I've searched around, but nobody seems to have a definitive script, and many of the answers on SO are contradictory. Here is the code I am currently using:
<?php
// Put your device token here (without spaces):
$deviceToken = [my device token];
// Put your private key's passphrase here:
// ITS REALLY THE PEM PASS
$passphrase = [the password];
// Put your alert message here:
$message = 'Hello, User!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
$cert = __DIR__ . '/Notifications_iOS/ck.pem';
stream_context_set_option($ctx, 'ssl', 'local_cert', $cert);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
/*
// Dev
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
*/
// Production
$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 have submit my app to apple store and now I want to send APNS to the users .
I want to send 7000 push messages through this php code from my desktop:
<?php
// Put your private key's passphrase here:
$passphrase = '*****';
// Put your alert message here:
$message = 'text 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.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</br>' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
////////////////////////////////////////////////////////////////////////
function selectfromdb(){
$x = array();
$con = mysql_connect("localhost","root","root");
mysql_select_db("my_db", $con);
mysql_query("set character_set_server='utf8'");
mysql_query("set names 'utf8'");
$result = mysql_query("SELECT idip FROM id");
$c = 0;
while($r = mysql_fetch_array($result))
{
$x[$c] = $r['idip'];
$c++;
}
return $x;
}
///////////////////////////////////////////////////////////////////////
$y = selectfromdb();
$i = 0;
while ($i < sizeof($y)){
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*',$y[$i]) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered</br>' . PHP_EOL;
else
echo 'Message successfully delivered</br>' . PHP_EOL;
$i++;
}
echo 'end</br>';
// Close the connection to the server
fclose($fp);
?>
but it just sent for 507 , then it says : Message not delivered
for all the rest .
The problem is that apple closes the connection after X messages that were sent to unavailable device-IDs.
Clean up your collection / database of deviceids by consuming the APNS feedback service, then finish the deployment of your APNS messages.
i use php to post to apple ..
$message = $error_msg;
$deviceToken = $dtoken;
$badge = 1;
$sound = 'received3.caf';
$body = array();
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '/home/administrator/applecert/apns-dev.pem');
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
error_reporting(E_ALL);
if (!$fp) {
print "Failed to connect $err $errstr\n";
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);
and andriod have similar way to post with php?
thanks ,all
Take a look at the C2DM service google offers :
http://code.google.com/intl/fr-FR/android/c2dm/
This code is well tested.
Note : You need to remember 3 points to check.
Passphrase : confirm with IOS developer.
.PEM : Please verify with your IOS develoepr created '.PEM' file is for sandbox or live server.
PORT 2195 : Need to verify whether this port has opened or not on your server.
If you have done these 3 steps, Now you can send push notification with below code with few configuration changes.
function pushNotification($deviceToken, $msg, $sounds, $type) {
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '');
// Put your private key's passphrase here:
$passphrase = ;
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$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);
$body['aps'] = array(
'alert' => $msg,
'sound' => $sounds,
'badge' => 1,
'type' => $type,
);
$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);
fclose($fp);
}