Web Notifications
API Endpoint Reference
API Base URL: http://sml.ms/restRegister web token
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
- Enable Google Cloud Messaging.
https://console.developers.google.com/apis/library/googlecloudmessaging.googleapis.com
- Generate a new API key.
Implement the web notification to the website
-
Include the main notification script file in the website. Download
<script src="web-notification.js"></script>
-
Add manifest file to the root of the website. Download
<link rel="manifest" href="/manifest.json">
- Modify the application name and ID in the manifest.json file to match the one created in Google API Console.
-
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
-
Registering user to service worker
navigator.serviceWorker.register('/sw.js').then(function(serviceWorkerRegistration) { //subscription goes here });
-
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 });
-
Generate endpoint which contains endpoint token, authKey and publicKey
var tokens = JSON.stringify(pushSubscription);
-
Send endpoint to the notification server
$.ajax({ type: 'POST', url: "/updateNotificationToken.php", data: { tokens: tokens, } })
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/restRegister 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 havingtoken
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 havingtoken
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
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 |
|
$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.