Hi
Has anyone had success in automating the sending of push notifications through some API or WebService?
We’ve implemented successfully the Fuse push notifications module but all the sending process is done manually with the firebase console for Android or the pusher app for iOS.
We need to push notifications programatically, any ideas?
What the meaning of Automation ?
Its could mean when something happens of the the Backend
, then push notification should be fired for all users right?
Okay, I have something like this on my Backend
projects. *Its very much easy to do this`.
For reference, Push notification providers contains Endpoint APIs; therefore there some sort of requests should be send to fire the notification.
For Android its too easy but iOS you need to issue a certification then convert it to be able to use it APNS
from Backend.
Can you tell me what the language used for Backend
? So i can send you code samples to simplify the process
Hi ahmed, thanks a lot for your reply
You’re right, when some event fire up in our Backend then we need to push the notification. The scope may be just one or several users.
We are using PHP as the backend language.
Hello again,
Good news, i have my Backend
also runs on PHP
I use this generic function for Push Notifications for Firebase
$API_ACCESS_KEY = 'PUT YOUR SERVER KEY --> goto firebase then open your projet and navigate to project settings and hit on Cloud Messaging tab and copy Server key value here';
$headers = array
(
'Authorization: key=' . $API_ACCESS_KEY,
'Content-Type: application/json'
);
$notification = [
"aps" => [
"alert" => [
"sound" => "default",
"title" => $title,
"body" => $msg,
"notificationStyle" => "bigtextstyle",
"notificationStyle" => "bigtextstyle",
"bigTitle" => $title,
"bigBody" => $msg,
"largeImage" => $userImage
],
"badge"=>$total
],
"payload" => [
"ux" => $ux,
"params" => $params
]
];
$fields = array(
'to' => $device_token,
'data' => $notification,
);
#Send Reponse To FireBase Server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
// echo $result;
curl_close($ch);
//redirect(base_lang('Home/Notification'));
//echo json_encode( $fields ) ;
return true;
For iOS APNS
you can use this code to push notification for iOS devices
$ctx = stream_context_create();
// ck.pem is your certificate file
$cert = APPPATH . 'third_party/server_certificates_bundle.pem';
stream_context_set_option($ctx, 'ssl', 'local_cert', $cert);
stream_context_set_option($ctx, 'ssl', '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);
// Create the payload body
$body = [
"aps" => [
"alert" => [
"title" => $title,
"body" => $msg
],
"sound" => "default"
],
"payload" => [
"ux" => $ux,
"params" => $params
]
];
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $device_token) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
// Close the connection to the server
fclose($fp);
If you’d like to create your own server_certificates_bundle.pem
for iOS you just have to follow this tutorial
Introduction
============
To send Push notification to an application/device couple you need an unique device token (see the [ObjectiveC demo](https://github.com/immobiliare/ApnsPHP/tree/master/Objective-C%20Demo)) and a certificate. The device token is generated by Apple from Device ID and Application ID, so this is unique per device and per application.
Generate a Push Certificate
============
To generate a certificate on a **Mac OS X**:
1. Log-in to the [iPhone Developer Program Portal](http://developer.apple.com/iphone/manage/overview/index.action)
1. Choose **App IDs** from the menu on the right ([or click here](http://developer.apple.com/iphone/manage/bundles/index.action))
1. Create an App ID without a wildcard. For example `3L223ZX9Y3.it.immobiliare.labs.apnsphp`
1. Click the **Configure** link next to this App ID and then click on the button to start the wizard to generate a new **Development Push SSL Certificate** ([Apple Documentation: Creating the SSL Certificate and Keys](http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ProvisioningDevelopment/ProvisioningDevelopment.html#//apple_ref/doc/uid/TP40008194-CH104-SW4))
1. Download this certificate and double click on `aps_developer_identity.cer` to import it into your Keychain
1. Launch **Keychain Assistant** (located in Application, Utilities or search for it with Spotlight) and click on My Certificates on the left
1. Expand **Apple Development Push Services** and select Apple Development Push Services **AND** your private key (just under Apple Development Push Services)
1. Right-click and choose "Export 2 elements..." and save as `server_certificates_bundle_sandbox.p12` (don't type a password).
1. Open **Terminal** and change directory to location used to save `server_certificates_bundle_sandbox.p12` and 1. convert the PKCS12 certificate bundle into PEM format using this command (press *enter* when asked for *Import Password*):
This file has been truncated. show original
Hi Ahmed, thanks for your great help.
I have all the ingredients in place so I think the test will be successful. I’ll keep you informed.
Greetings from Mexico
Ahmed
As you said, Android was a breeze to implement.
For iOS, we were able to connect to the server “api.push.apple.com ” with 443 or 1297 port (https://web-people.co/t/ios-push-notifications-with-php/19/3 ).
We’re getting a 150 return code, which doesn’t appear at the official documentation.
We also found the ApnsPHP project, have you used it already?
I was using it and recently i’ve changed it.
The code i’ve sent is already working for me, you have to generate fine certification to let it work