Send push notification to a group - php

I am not sure how to send a push notification to multiple tokens. With my script it seems to only send a push notification to one... as per Apple it would be better to have one open connection and keep it open and send messages when needed. Not sure how to do it on my php server...
$payload['aps'] = array('alert' => "New Cave report for ".$caveName,'badge' => 1, 'sound' => 'default');
$payload['condition'] = array('conditionID' => $ccID, 'caveName' => $caveName);
$payload = json_encode($payload);
// Connection Part
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = '/var/www/web543/files/apns-dev.pem';
if (!file_exists($apnsCert) )
{
echo "Certification file not found!";
} else
{
$streamContext = stream_context_create();
stream_context_set_option($streamContext,'ssl','local_cert',$apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort,$error,$errorString,2,STREAM_CLIENT_CONNECT,$streamContext);
if ( !$apns )
{
echo "Connection Failed!".$errorString;
} else
{
$db->query("SELECT DISTINCT token FROM notifications WHERE userID != '{$userID}'");
if ($db->num_rows()>0)
{
while ($db->next_record())
{
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $db->f('token'))) . chr(0) . chr(strlen($payload)) . $payload;
if ( fwrite($apns, $apnsMessage) === FALSE )
{
echo "Can not write";
}
}
}
}
fclose($apns);
}
some output of $apnsMessage
Token: 5a984922e19eab54f78fd54e24d5b02a3d30ccdbbeee34aadbdacaa687ee1261 Message: Z˜I"áž«T÷ÕN$Õ°*=0ÌÛ¾î4ªÛÚʦ‡îa†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
Token: c607acc70bd4885bf56f3b4827523023bf93a1d644626768ab0304bb3b4414dc Message: ƬÇÔˆ[õo;H'R0#¿“¡ÖDbgh«»;D܆{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
Token: 785ec3128972bd3d4c3e6fa1eeead97b73b0696e2361339a2467e6ba775b83ea Message: x^Ér½=L>o¡îêÙ{s°in#a3š$gæºw[ƒê†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
Token: c592487e3c71e921d0b7a825b66ed5e58070fee709131535ac391f14febbcfdc Message: Å’H~Token: 061bc20ba3a0fc17c689e052b42b5789f502a52d43180ea114e3212077045315 Message: £ üƉàR´+W‰õ¥-C¡ã! wS†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
Token: 26fb66fef67a122ca456f106363115285d4d7156e7c8ab6e51bd5bfa9bab2d03 Message: &ûfþöz,¤Vñ61(]MqVçÈ«nQ½[ú›«-†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}

Ok. I found the problem.
When compiling as Debug it will send only on the first device when using the DEV certification. If I use Deployment like Ad-Hoc I have to use the Prod Servers and Prod Certificates.
Now everything works.

I think you have to run this script in loop according to the count of token.
Means first get the token array and then according to the count make a loop. Then take one device token from the index 0 and run this script. Then selcect another token at index 1 and then run again this script.
To send push notification to multiple device you have connect each time to apple server and close connection and again open and send.This is the process.

Yeah you should not be making and tearing down the socket connection every time you need to send the push. Once you establish the socket connection, you can write as many as push messages you want to any number of devices.
What is the exact problem that you are observing? Is your socket connection getting dropped?
Have a look at this question if you need additional help : How to send Push Notification to multiple devices?

Related

APNS not working, SSL Broken Pipe & bad write retry

I'm developing apple application for my company,
I'm working on server side using PHP, laravel framework to send message to APNS.
For testing, I use hardcode device token.
I get it work on one device that I hardcode.
so I'm advance to function that send apns, a device token that requested in function.
so when I call this function, it get device token from database and send to apns.
I get error
fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry
and
fwrite(): SSL: Broken pipe
I change the code back to first testing, but it does not work like before.
anyone have idea what I made wrong or did I made a mistake in my request?
for note this is my code
$apnsServer = 'ssl://gateway.sandbox.push.apple.com:2195';
$privateKeyPassword = 'my_private_key';
$id = $request->input('id');
$message = "message_here";
$userSelected = User::select('device_token')->where('id', $id)->first();
$deviceToken = str_replace(' ', '', $userSelected['device_token']);
$pushCertAndKeyPemFile = $_SERVER['DOCUMENT_ROOT'].'my_certificate.pem';
$stream = stream_context_create();
stream_context_set_option($stream, 'ssl', 'passphrase', $privateKeyPassword);
stream_context_set_option($stream, 'ssl', 'local_cert', $pushCertAndKeyPemFile);
$connectionTimeout = 30;
$connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
$connection = stream_socket_client($apnsServer, $errorNumber, $errorString, $connectionTimeout, $connectionType, $stream);
if (!$connection){
echo "Failed to connect to the APNS server. Error = $errorString <br/>";
exit;
}else{
echo "Successfully connected to the APNS. Processing...</br>";
}
$messageBody['aps'] = array('alert' => $message, 'sound' => 'default', 'badge' => 1);
$payload = json_encode($messageBody);
$notification = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$wroteSuccessfully = fwrite($connection, $notification, strlen($notification));
if (!$wroteSuccessfully){
echo "Could not send the message<br/>";
} else {
echo "Successfully sent the message<br/>";
}
After some research, i've found my problem.
When our server send PUSH message to APNS, there is response error need to check for every request.
After check error, i get error message 8, invalid token
When using development certificate, and there is user register using development application, they get device token
but when you change to production, you have to change production certificate and registered user need to get new device token to make PUSH notification work
because device token you get in development apps, will not work with production certificate..
it's totally my mistake to miss important things..

Apple Push Notifications System Not Receving Message! (PHP)

This is the code I wrote to send apple push notifications.
in the device_tokens i'm sending an array with all of the tokens I want to send message to.
For some reason, I stopped recieving messages from this system.
and it used to work before!
private function send_iphone_notification($device_tokens, $message)
{
define("PRODUCTION_MODE", true);
$apnsPort = 2195;
// Choose dev or production apns host and certificate
if(PRODUCTION_MODE) {
$apnsHost = 'gateway.push.apple.com';
$apnsCert = $_SERVER['DOCUMENT_ROOT'].'/ant/www/apns/PushIphone.pem';
} else {
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsCert = $_SERVER['DOCUMENT_ROOT'].'/ant/www/apns/CertificatesDev.pem';
}
// Notification content
$payload = json_encode(array('notification_from_panel' => 1, 'aps' => array('alert' => $message, 'badge' => 1, 'sound' => 'default')));
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($streamContext, 'ssl', 'passphrase', "");
// loop all devices tokens
foreach ($device_tokens as $device_token)
{
try {
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
} catch(Exception $e) {
echo $e->getMessage();
continue;
}
$deviceToken = str_replace(" ","",substr($device_token,1,-1));
//echo $deviceToken;
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(mb_strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
//socket_close($apns);
fclose($apns);
}
}
I have some experience with Apple's push service and i have to say that apple seems to have soem restrictions in place.
First of all you dont need to close and open the socket connection for every message. That will save you a lot of time. On the other hand you have to check if the connection is broken before you send the next message.
Anyway, it seemed that apple does not like it much when you connect a hundred times a second and send messages. Apples has blocked me for that as well in the passed.
But when you send the message with a already established connection, and at some point the connection is returning an error/ is disconnected, you dont know at what message he stopped. But you can define a identifier for each message and check the error returned when the connection is broken.
I noticed that apples server returns the error with a couple of seconds delay. That makes it hard do deal with it the easy way.
I have written a class to handle all of that but did not jet have time to publish it! If you are interested in it, check the github project for it at https://github.com/tinned-software/PHP-Tinned-MobilePush
I hope to finish the documentation soon.

APNS not sending response nor sending push notification

Apple's docs say that:
"If you send a notification and APNs finds the notification malformed or otherwise unintelligible, it returns an error-response packet prior to disconnecting. (If there is no error, APNs doesn’t return anything.) Figure 5-3 depicts the format of the error-response packet."
This leads me to believe that the only reason that APNS would not send something back is if what I sent them was in the correct format. However when I try to fread for their response I get a string of 0 length and when unpacked becomes null, which I assume means nothing has been written back to me.
I the stream was opened by stream_socket_client() and did not return false or throw an exception. I know that my fwrite successfully wrote 154 bytes to that stream as well. Why would there be no response from Apple?
Here is the code for connecting to APNS:
function openConnection() {
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $this->APNS_CERT);
stream_context_set_option($streamContext, 'ssl', 'passphrase', $this->Password);
$apns = stream_socket_client('ssl://' . $this -> APNS_HOST . ':' . $this -> APNS_PORT, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
return $apns;
}
Then, in the next method after calling openConnection:
//open the connection to use with apple.
$apns = $this->openConnection();
$alert = $message['alert'];
$badge = $message['badge'];
$deviceToken = $message['deviceToken'];
$payload['aps'] = array(
'alert' => $message['alert'],
'badge' => $message['badge'],
'sound' => $message['sound']
);
if ($message['extraPayload'] != null) {
$payload['acme'] = $message['extraPayload'];
}
$encodedString = json_encode($payload);
//create message
$apnsMessage = chr(1) . pack("N", $message['identifier']) . pack("N", $message['expire']) . pack("n", 32) . pack('H*', str_replace(' ', '', $message['deviceToken'])) . pack("n",strlen($encodedString)) . $encodedString;
$write = fwrite($apns, $apnsMessage);
echo $write;
//the echo was just to see if it wrote.
if (!$apns) {
socket_close($apns);
fclose($apns);
echo "connection to APNS was lost.";
}
//look for changes. $null=null because some bug doesn't just let you pass null.
$null = null;
$changedStreams = stream_select($streamArray, $null, $null, 0, 1000000);
//check if it is actually false
if ($changedStreams === false) {
//close stream when done.
socket_close($apns);
fclose($apns);
echo "No response from APNs";
} elseif ($changedStreams > 0) {
//then check if what they sent back is an error and grab the error packet
$responseBinary = fread($apns, 6);
var_dump($responseBinary);
//check that it's the right thing
if ($responseBinary != false || strlen($responseBinary) == 6) {
//convert it from it's binary stream state and print.
$response = unpack('Ccommand/Cstatus_code/Nidentifier', $responseBinary);
var_dump($response);
//close stream when done.
socket_close($apns);
fclose($apns);
}
} else {
echo "Apple failed to respond, message was not sent.";
}
The var_dump at the end is NULL.
Edit:
Turns out it was an error with conflicting credentials. It was solved by creating a new pem file.
From that page:
You should regularly connect with the feedback web server and fetch the current list of those devices that have repeatedly reported failed-delivery attempts. Then you should cease sending notifications to the devices associated with those applications. See “The Feedback Service” for more information.
Feedback Service
Access to the feedback service takes place through a binary interface similar to that used for sending push notifications. You access the production feedback service via feedback.push.apple.com, port 2196; you access the sandbox feedback service via feedback.sandbox.push.apple.com, port 2196. As with the binary interface for push notifications, you must use TLS (or SSL) to establish a secured communications channel. The SSL certificate required for these connections is the same one that is provisioned for sending notifications. To establish a trusted provider identity, you should present this certificate to APNs at connection time using peer-to-peer authentication.

iOS Push notifications with passbook

I'm currently writing a passbook API that allows users to send push notifications to passes. Now I'm at the point of testing it but my push notification aren't arriving. I'm writing the push sending code in PHP and this is what I have so far:
$payload = json_encode(array("aps" => array("alert" => "test", "sound" => "default")));
//send it to all devices found
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
error_log($row['pushToken']);
//write the push message to the apns socket connection
$msg = chr(0) .
pack("n",32) .
pack('H*', $row['pushToken']) .
pack("n",strlen($payload)) .
$payload;
fwrite($fp, $msg);
}
Is there anything I'm missing here or doing wrong? The certificate is valid and connecting to the APNS server doesn't give me any error.
Thanks!
I've solved this problem by re-creating the certificate, doing the openssl magic again and use the new certicates. Now it's working, so I think there was something bugged in my certificate :)

Server Side iPhone APNS Communication

I have a quick question. For the apple push notification service, I need to have my own server, which will send out the push notifications to the apple servers, right?
Can I do this with a simple PHP script on a regular webhosting account, or do I need a dedicated server with full blown admin access for that?
If this is possible in PHP, can anyone point me to some samples that can help me get started on this? Right now, I am pretty confident I won't have trouble implementing the client-side part, but the server side is still somewhat of a mystery to me...
Thank you!
Florian
You probably will be able to do this on a limited hosting account, as long as you can leave the connection open to the server most of the time. Some sample code:
http://code.google.com/p/php-apns/
Note also that some companies are starting up services to help you specifically with push hosting (I'll keep the post neutral and not mention names, I'm not sure which services are running just yet).
The Main problem with APNS is ports
so many providers doesnt open 2195 port
so concentrate on that initially then go for the host provider
Here is the code what i tried, but one problem is not able to get device notification
Hi ,
i tried the following code (PHP)
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apple_push_notification_production.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
if($apns)
{
echo "Connection Established<br/>";
$deviceToken = '**********';//masked
$body = array();
$body['aps'] = array(’alert’ => "test message");
//$body['aps']['badge'] = 1;
$payload = json_encode($body);
$apnsMessage = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "sending message :" . $apnsMessage . "<br/>";
print "sending payload :" . $payload . "<br/>";
fwrite($apns, $apnsMessage);
}
else
{
echo "Connection Failed";
echo $errorString;
echo $error;
}
socket_close($apns);
fclose($apns);
reply is Connection Established
sending message :�� d^÷Îå0ZCd%1ÄuwOOYš'ÊÈ}ârðm¾Í�,{"aps":{"\u2019alert\u2019":"test message"}}
sending payload :{"aps":{"\u2019alert\u2019":"test message"}}
But am not able to get the notification
any help?

Categories