Progress will discontinue Telerik Platform on May 10th, 2018. Learn more

Constructing a Notification Object

Constructing a Notification Object

Unless you want to send a basic generic message to all registered devices, you need to construct a JSON notification object to pass as a payload to the send request.

You can include the following sections in the push notification object:

  • Filter expression: The Filter option is a generic field that allows you to target a subset (segment) of your devices with the push notification. For detailed discussion of filters and targeting, see Targeting Push Notifications.
  • Generic options: Options that apply to all types of devices. Push Notification Object Field Reference provides a list of all supported generic options.
  • Vendor-specific options: Options that apply only to certain devices such as iOS or Android. Push Notification Object Field Reference provides a list of all supported vendor-specific options.

The next JSON object includes all three sections: Filter, the UseLocalTime generic option, and a section for each supported device vendor:

var notification = new PushNotification();

var iosNotificationPayload = new Dictionary<string, object>()
{
    {
            "aps",
            new Dictionary<string, object>()
            {
                { "alert", "message for iOS" },
                { "sound", "default" },
                { "badge", 1 },
                { "category", "MY_CATEGORY" }
            }
    },
    {
            "customData",
            new Dictionary<string, object>()
            {
                { "specialkey", "specialvalueforiOS" }
            }
    }
};

//Android
var androidNotificationPayload = new Dictionary<string, object>()
{
    {
            "data",
            new Dictionary<string, object>()
            {
                { "title", "title for Android" },
                { "message", "Push message for Android" },
                {
                        "customData",
                        new Dictionary<string, object>()
                        {
                            { "specialkey", "specialvalueforAndroid" }
                        }
                }
            }
    }
};

//Windows Phone
var windowsPhoneToastNotification = new Telerik.Everlive.Sdk.Core.Model.System.Push.WindowsPhone.ToastNotification("Toast Title", "Toast Message", null);

//Windows
JObject windows = new JObject();
JObject tile = new JObject();
tile["template"] = "TileSquarePeekImageAndText01";
tile["text"] = new JArray(new String[] { "MyText1", "MyText2", "MyText3", "MyText4" });

notification.iOS = new Telerik.Everlive.Sdk.Core.Model.System.Push.IOS.IOSNotification();

foreach (KeyValuePair<string, object> pair in iosNotificationPayload)
{
    notification.iOS.SetValue(pair.Key, pair.Value);
}

notification.Android = new Telerik.Everlive.Sdk.Core.Model.System.Push.Android.AndroidNotification();
foreach (KeyValuePair<string, object> pair in androidNotificationPayload)
{
    notification.Android.SetValue(pair.Key, pair.Value);
}

notification.Windows = windows;
notification.WindowsPhone.Toast = windowsPhoneToastNotification;

Dim notification = New PushNotification()

Dim iosNotificationPayload = New Dictionary(Of String, Object)() From { _
    {"aps", New Dictionary(Of String, Object)() From { _
        {"alert", "message for iOS"}, _
        {"sound", "default"}, _
        {"badge", 1}, _
        {"category", "MY_CATEGORY"} _
    }}, _
    {"customData", New Dictionary(Of String, Object)() From { _
        {"specialkey", "specialvalueforiOS"} _
    }} _
}

'Android
Dim androidNotificationPayload = New Dictionary(Of String, Object)() From { _
    {"data", New Dictionary(Of String, Object)() From { _
        {"title", "title for Android"}, _
        {"message", "Push message for Android"}, _
        {"customData", New Dictionary(Of String, Object)() From { _
            {"specialkey", "specialvalueforAndroid"} _
        }} _
    }} _
}

'Windows Phone
Dim windowsPhoneToastNotification = New Telerik.Everlive.Sdk.Core.Model.System.Push.WindowsPhone.ToastNotification("Toast Title", "Toast Message", Nothing)

'Windows
Dim windows As New JObject()
Dim tile As New JObject()
tile("template") = "TileSquarePeekImageAndText01"
tile("text") = New JArray(New [String]() {"MyText1", "MyText2", "MyText3", "MyText4"})

notification.iOS = New Telerik.Everlive.Sdk.Core.Model.System.Push.IOS.IOSNotification()

For Each pair As KeyValuePair(Of String, Object) In iosNotificationPayload
    notification.iOS.SetValue(pair.Key, pair.Value)
Next

notification.Android = New Telerik.Everlive.Sdk.Core.Model.System.Push.Android.AndroidNotification()
For Each pair As KeyValuePair(Of String, Object) In androidNotificationPayload
    notification.Android.SetValue(pair.Key, pair.Value)
Next

notification.Windows = windows
notification.WindowsPhone.Toast = windowsPhoneToastNotification

Telerik Platform uses the information to send the appropriate push notification to each targeted device. If you fail to specify options for a given vendor, its devices are served from the generic options. For example the next object sends a generic message to all devices except for iOS devices. Note that in this case the Message option is required.

var notification = new PushNotification("This text will appear on all devices except for iOS devices");

//iOS
var iosNotificationPayload = new Dictionary<string, object>()
{
    {
            "aps",
            new Dictionary<string, object>()
            {
                { "alert", "This text will override the generic Message on iOS" },
                { "sound", "default" },
                { "badge", 1 },
                { "category", "MY_CATEGORY" }
            }
    },
    {
            "customData",
            new Dictionary<string, object>()
            {
                { "specialkey", "specialvalueforiOS" }
            }
    }
};

notification.iOS = new Telerik.Everlive.Sdk.Core.Model.System.Push.IOS.IOSNotification();
foreach (KeyValuePair<string, object> pair in iosNotificationPayload)
{
    notification.iOS.SetValue(pair.Key, pair.Value);
}
Dim notification = New PushNotification("This text will appear on all devices except for iOS devices")

'iOS
Dim iosNotificationPayload = New Dictionary(Of String, Object)() From { _
    {"aps", New Dictionary(Of String, Object)() From { _
        {"alert", "This text will override the generic Message on iOS"}, _
        {"sound", "default"}, _
        {"badge", 1}, _
        {"category", "MY_CATEGORY"} _
    }}, _
    {"customData", New Dictionary(Of String, Object)() From { _
        {"specialkey", "specialvalueforiOS"} _
    }} _
}

notification.iOS = New Telerik.Everlive.Sdk.Core.Model.System.Push.IOS.IOSNotification()
For Each pair As KeyValuePair(Of String, Object) In iosNotificationPayload
    notification.iOS.SetValue(pair.Key, pair.Value)
Next

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.