Sending Push Notifications

Sending Push Notifications

The send operation allows you to send either a generic push notification to all recipients or a customized, targeted push notification based on a JSON notification object.

Despite its name, the send operation only schedules push notifications for sending. If you have not specified sending time, the server dispatches the push notification immediately or almost immediately. If a sending time is available, the push notification is send accordingly.

You can choose between the following methods for sending push notifications:

Using the Portal

The Telerik Platform portal allows you to compose and target push notification using a graphical user interface (GUI). It provides a rich set of filtering and scheduling options, as well as tools to monitor the progress and the state of your push notifications. Filters and options that are not available in the GUI must be used through one of the client SDKs or the RESTful API.

The GUI is suitable for sending single push notification. When you need to send large numbers or regular push notifications, you may find it more convenient to use the other sending methods.

To create a push notification using the portal:

  1. Open you app.
  2. Navigate to Notifications > Push Notifications > Push Browser.
  3. Click the Send a push notification button.
  4. In the details panel that appears, fill in your details and select options.
  5. Click Send Now.

This adds a row to the push browser. Clicking it opens the details pane where you can track the state of the push notification.

Using the RESTful API

Sending a push notification using the RESTful API is much the same as creating a new data item. You pass the notification object as a payload.

Generic messages are defined by setting the notification object's Message field. They are treated differently on different platforms because of platform specifics. For example a generic message is sent to iOS devices as an alert which displays on the screen.

Examine the following code to learn how to create a notification with a generic message addressing all registered devices:

Request:
    POST https://api.everlive.com/v1/your-app-id/Push/Notifications
Headers:
    Content-Type: application/json
Payload: raw
{
    "Message": "A generic message"
}
Response:
    Status: 201 Created
    Content-Type: application/json
<?php
$curl_url = 'https://api.everlive.com/v1/your-app-id/Push/Notifications';

$payload = array(
    'Message' => 'A generic message'
);
$content = json_encode($payload);

$handler = curl_init();

curl_setopt($handler, CURLOPT_URL, $curl_url);
curl_setopt($handler, CURLOPT_HTTPHEADER,
    array("Content-type: application/json"));
curl_setopt ($handler, CURLOPT_POST, true);
curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, false); // Either use this line to skip the verification of the Telerik Platform SSL server certificate (not recommended) or add the certificate to the cURL trusted CA store. See the official cURL documentation for details.
curl_setopt($handler, CURLOPT_POSTFIELDS, $content);

$response = curl_exec ($handler);

curl_close($handler);
?>

If you want to target a subset of your devices or to specify advanced options, you need to expand the payload notification object accordingly. This is discussed in detail in Constructing a Notification Object.

Using Cloud Code

Sending push notifications using Cloud Code has certain benefits compared to the other methods.

The benefits include:

  • You can use the Cloud Code for Data events to trigger automatic sending of push notifications when something in your data changes.

    Cloud Code for Data events are only triggered when data is changed trough the Backend Services APIs. For (Data Connectors, this means that no events will be triggered if you change the data directly in the connected database or through another application that does not utilize the Backend Services APIs.

  • When writing a Cloud Function to send push notifications you can safely use your master key to invoke operations that require it because it does not leave Telerik Platform. You can then invoke the Cloud Function's HTTP endpoint using only your App ID.

  • You can use the Backend Services JavaScript SDK or the RESTful API.

The following example demonstrates how to use the Backend Services JavaScript SDK to send a simple push notification from the beforeRead Cloud Code for Data event:

Everlive.Events.beforeRead(function(request, context, done) {
    Everlive.Sdk.$.push.notifications.create({ Message:'Hello' },
        function(error, response) {
            if (error){
                //Process the error here
            } else {
                //Process the response here
            }
            done();
        });
});

This code above schedules the "Hello" message for sending to all devices registered for push notifications in your application. If you want to target a subset of your devices or to specify advanced options, you need to construct the payload notification object accordingly:

Everlive.Events.beforeRead(function(request, context, done) {
    var notification = {
        //place targeting and other options here
    };
    Everlive.Sdk.$.push.notifications.create(notification,
        function(error, response) {
            if (error){
                //Process the error here
            } else {
                //Process the response here
            }
            done();
        });
});

Constructing the notification object is discussed in detail in Constructing a Notification Object.

You can find more information about using the RESTful API in the RESTful section of the documentation.

See Also

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.