Web Notifications

API Endpoint Reference

API Base URL: http://sml.ms/rest

Register web token

Use this endpoint to register a web token in the database.

Endpoint

http://sml.ms/rest/newToken/Web

Request parameters

The parameters must be passed as a JSON array in the request body having token as a key. For generating the endpoint on the client side refer to the implementation section

Key Value Type Value Description
applicationId integer Refer to the applications page.
token string Alphanumeric string.
publicKey string
authKey string
deviceId integer Device ID.
status boolean 1 for enabled. 0 for disabled.
level integer 1-4.
categories array Optional. Array of section ID and level pairs.
browser string Chrome or Firefox
browserVersion integer Version of the browser from which the user registered
platform string Desktop or Mobile
 $params= array();
 $params["applicationId"]=10;
 $params["token"] = "https://android.googleapis.com/gcm/send/f2FfAf6MvU4:APA91bFgMd0wNUM2MuYGLW2KhlxcYADRjn2gvlcfnXNv5VTaK2bn4wSsdFrSj7zj5cW-ORyxfzqc9tCt76WIowXfxhOtoAVyuBmvPxEwG7UMbvzzSZZ4PE2Ikgz2MRMVXLFAYbRTuX61";
 $params["publicKey"] = "jejle1zFN5VjF3mFk-TjRw==";
 $params["authKey"] = "BKhvlguPtl01bCe6gLwhnMAsTQIJkcowhrM7MW1vx1RGwLxpJApfjvjEwAphSidu0vi7auxuBec37kGlNA_s938=";
 $params["deviceId"] = "a6ec2d53cd797317a42535fb2b416047";
 $params["status"] = 1;
 $params["level"] = 4;
 $params["categories"] = array(33=>2,65=>3);
 $params["browser"] = "Chrome"
 $params["browserVersion"] = 63
 $params["platform"] = "Desktop"

 $url = "http://sml.ms/rest/newToken/Web";
 $res = send_curl($url,array("token"=>json_encode($params)));

To stop receiving notifications indefinitely, send a request with a status of 0.

Implementation

Create a new project in Google API Console

  1. Enable Google Cloud Messaging.
    https://console.developers.google.com/apis/library/googlecloudmessaging.googleapis.com
  2. Generate a new API key.

Implement the web notification to the website

  1. Include the main notification script file in the website. Download
    <script src="web-notification.js"></script>
  2. Add manifest file to the root of the website. Download
    <link rel="manifest" href="/manifest.json">
  3. Modify the application name and ID in the manifest.json file to match the one created in Google API Console.
  4. Add the service worker script to the root directory of the website. Download
    sw.js

Subscribe and register user to push notifications

Requesting permission and generating browser endpoint

Registering user to push notifications and generating tokens requires the following steps

  1. Registering user to service worker
                                navigator.serviceWorker.register('/sw.js').then(function(serviceWorkerRegistration) {
                                      //subscription goes here
                                });
                            
  2. Subscribe user and requesting permission to send push notifications
                                serviceWorkerRegistration.pushManager.subscribe({userVisibleOnly: true}).then(function(pushSubscription) {
                                     //generate endpoint and send it to server here
                                });
                            
  3. Generate endpoint which contains endpoint token, authKey and publicKey
    var tokens = JSON.stringify(pushSubscription);
  4. Send endpoint to the notification server
                                $.ajax({
                                  type: 'POST',
                                  url: "/updateNotificationToken.php",
                                  data: {
                                    tokens: tokens,
                                  }
                                })
                            
    Refer to the first section (Register web token) for the server side token registration implementation
Here's how the final code will look like
                navigator.serviceWorker.register('/sw.js').then(function(serviceWorkerRegistration) {
                  serviceWorkerRegistration.pushManager.subscribe({userVisibleOnly: true}).then(function(pushSubscription) {

                    var tokens = JSON.stringify(pushSubscription);

                    $.ajax({
                      type: 'POST',
                      url: "/updateNotificationToken.php",
                      data: {
                        tokens: tokens,
                      }
                    })

                  ).catch(function(error) {
                    flagSubcribtionAsUnsubscribed();
                  });
                });
                

App Notifications

API Endpoint Reference

API Base URL: http://sml.ms/rest

Register Android device

Use this endpoint to register an Android device to the list of devices that wish to receive notifications.

Endpoint

http://sml.ms/rest/newToken/Android

Request Parameters

The parameters must be passed as a JSON array in the request body having token as a key.
Key Value Type Value Description
applicationId integer Refer to the applications page.
token string Alphanumeric string.
level integer 1-4.
deviceId integer Device ID.
status boolean 1 for enabled. 0 for disabled.

Register iPhone device

Use this endpoint to register an iPhone device to the list of devices that wish to receive notifications.

Endpoint

http://sml.ms/rest/newToken/Iphone

Request Parameters

The parameters must be passed as a JSON array in the request body having token as a key.
Key Value Type Value Description
applicationId integer Refer to the applications page.
token string Alphanumeric string.
level integer 1-4.
deviceId string Device Name.
status boolean 1 for enabled. 0 for disabled.
sound string Optional. Defaults to default.

Push a new notification to server

Use this endpoint to push a new notification to the server after inserting a news in the database.

Endpoint

http://sml.ms/rest/newNotification

Request Parameters

The parameters must be passed as a JSON array in the request body having notification as a key.

Key Value Type Value Description
applicationId integer The application ID which can be found in the applications page.
level integer 1-4.
newsId integer The submited news ID
data array
Key Value Type Value Description
id integer News ID.
Date datetime News create date.
Image string News image URL.
newsLevel integer 1-9.
bigImage string News image URL.
Title string News title.
Body string News body.
Category integer News category ID.
videoId integer Video ID if news has video.
video integer 1 if news has video.
 $params= array();
 $params["applicationId"]= 10;
 $params["level"] = 3;
 $params["newsId"] = 322869;
 $params["data"]["id"]= 322869;
 $params["data"]["Date"]= "2017-12-13 10:54:16";
 $params["data"]["Image"]= "https://files.elnashra.com/elnashrafinance/imagine/pictures_115_92/2956998_1482918276.jpg";
 $params["data"]["Title"]= "This is the news title";
 $params["data"]["Body"]= "This is the news body";
 $params["data"]["Category"]= 33;

 $url = "http://sml.ms/rest/newNotification";
 $res = send_curl($url,array("notification"=>json_encode($params)));

As soon as the notification is submitted, the notification server will check the application that has the same id as the submitted application id and check what platform the application support, then inserts the notification according to the platform

The notification will be splitted into multiple queues with maximum 100 token then sent to the user

After the notification is sent, its status will be updated from pending to sent.