Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apple push notification with cURL

Is there a way to implement in PHP a posting push notification system by using cURL (instead of stream_socket_client) ?

I wrote :

$url = 'https://gateway.sandbox.push.apple.com:2195';
$cert = 'AppCert.pem';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSLCERT, $cert);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "passphrase");

$curl_scraped_page = curl_exec($ch);

But how to set the device token and the json message ?

like image 339
Pierre Avatar asked Jan 07 '13 17:01

Pierre


People also ask

How do I test Apple push notifications?

To send a test push, go to Mobile Apps > Integrations > Test Push. Use the Apple Sandbox Certificates for Xcode and the Apple Production Certificates when testing from TestFlight.

How do I send push notifications to iOS?

Go back to the iOS project you created earlier and select the main target. Under the Signing & Capabilities tab, click on the + Capability button, then select Push Notifications. This will enable your app to receive push notifications from OneSignal. Next, you'll need to add a Notification Extension to the app.

Can we test push notifications on iOS simulator?

As of Xcode 11.4, users are finally able to test Push Notifications via the iOS Simulator. We can send push notifications to the simulator in two different ways: Use a command provided by Xcode Command Line Tools in Terminal. Drag and drop an APNs file onto the target simulator.


2 Answers

Here is the code for device token and json message,I think this will help you.

$url = 'https://gateway.sandbox.push.apple.com:2195';
$cert = 'AppCert.pem';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSLCERT, $cert);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "passphrase");
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"device_tokens": ["458e5939b2xxxxxxxxxxx3"], "aps": {"alert": "test message one!"}}');

$curl_scraped_page = curl_exec($ch);
like image 177
vasudev Avatar answered Sep 19 '22 12:09

vasudev


This code works

$deviceToken = '...';
$passphrase = '...';
$message = '...';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'xxx.pem');
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);
$body['aps'] = array('alert' => $message,'sound' => 'default');
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
fclose($fp);
like image 26
user2469560 Avatar answered Sep 21 '22 12:09

user2469560