Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending notification using PHP to IOS devices

Tags:

php

xcode

ios

i have an app that receives push notification which build on Xcode using Objective-c. I did follow this tutorial this tutorial by Ray Wenderlich, the notification work fine and I can receive immediate message. I have my own web site and I want to use it to send the notification using PHP. I did try to find a tutorial about that but I could not.

Is it possible to use my web site to send the notification? CAN I use PHP file to send the notification? Do I need to change the SSl Certificate to a Production SSL?

Her is the PHP code which I use at the moment:

<?php

$deviceToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
// Put your private key's passphrase here:
$passphrase = 'xxxxxxxxxx';

// Put your alert message here:
$message = 'Hello app';



$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);

?>

I hope my question is clear, and hope some one to give me some information or to direct me to a tutorial that could help me.

Thanks

like image 385
Ahmed.S.Alkaabi Avatar asked Aug 06 '26 13:08

Ahmed.S.Alkaabi


1 Answers

Yes you can certainly send push notifications to iOS devices using PHP. But before sending any notification, you will be needing:

1) Apple Push Certificate (APNs Certificate) - This can be generated from Apple Developer Center. Note that the certificate is per app and there are two kind of certificates, Development and Distribution. Follow This Link on how to generate one.

2) Device Token for you device - This is to be done on your iOS app. You code on iOS App might look something like this.

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    self.storedDeviceToken = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    self.storedDeviceToken = [self.storedDeviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"My token is: %@", self.storedDeviceToken);
}

Now that you have all the tools required for push notification, you can start with your PHP script.

// Put your device token here (without spaces):
$deviceToken = 'XXXXXXXXXXXXXXX';

$gateway = 'ssl://gateway.push.apple.com:2195';

// Put your private key's passphrase here:
$passphrase = 'YourPassphrase!!';

// Put your alert message here:
$message = 'Yoooo! What\'s up man!';

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'PathToGeneratedCertificateOnStep1');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
    $gateway, $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);

For more details on the payloads supported by Apple Push Notification visit.

NOTES:

  • If you are installing the iOS app manually for testing then the device token is only valid for sandbox notification. So, your $gateway will be 'ssl://gateway.sandbox.push.apple.com:2195'

  • Notification cannot be received on simulator.

like image 169
Arun Poudel Avatar answered Aug 08 '26 01:08

Arun Poudel