Push Notification automation through API

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

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