wns object

 

Enables you to send push notifications to a Windows Store or universal Windows app from Microsoft Azure Mobile Services server scripts using Azure Notification Hubs.

Methods

  • createNativeRegistration(channel, tags, optionsOrCallback, callback)
    Creates a native WNS registration.

    Parameters

    Name

    Type

    Argument

    Description

    channel

    string

    The device channel URI.

    tags

    string | array

    A single tag or array of tags (comma-separated list, no spaces).

    options

    object

    <optional>

    The request options or callback.

    callback

    object

    <optional>

    A JSON object that specifies the success and error callback functions.

  • createOrUpdateNativeRegistration(registrationId, channel, tags, optionsOrCallback, callback)
    Creates or updates a native WNS registration.

    Parameters

    Name

    Type

    Argument

    Description

    registrationId

    string

    The registration identifier.

    channel

    string

    The device channel URI.

    tags

    string | array

    A single tag or array of tags (comma-separated list, no spaces).

    options

    object

    <optional>

    The request options or callback.

    callback

    object

    <optional>

    A JSON object that specifies the success and error callback functions.

  • createOrUpdateRawTemplateRegistration(registrationId, channel, tags, template, optionsOrCallback, callback)
    Creates or updates a custom template registration. Use this method to have full control on the template format. You must also specify the X-WNS-Type header (possible values: wns/toast, wns/tile, wns/badge, wns/raw).

    Parameters

    Name

    Type

    Argument

    Description

    registrationId

    string

    The registration identifier.

    channel

    string

    The device channel URI.

    tags

    string | array

    A single tag or array of tags (comma-separated list, no spaces).

    template

    string

    The XML template for the registration.

    options

    object

    <optional>

    The request options or callback.

    Name

    Type

    Argument

    Description

    pnsCredentialName

    object

    <optional>

    The pns credentials to use.

    headers

    object

    <optional>

    The wns headers to include.

    callback

    object

    <optional>

    A JSON object that specifies the success and error callback functions.

  • createRawTemplateRegistration(channel, tags, template, optionsOrCallback, callback)
    Creates a custom template registration. Use this method to have full control on the template format. You must also specify the X-WNS-Type header (possible values: wns/toast, wns/tile, wns/badge, wns/raw).

    Parameters

    Name

    Type

    Argument

    Description

    channel

    string

    The device channel URI.

    tags

    string | array

    A single tag or array of tags (comma-separated list, no spaces).

    template

    string

    The XML template for the registration.

    options

    object

    <optional>

    The request options or callback.

    Name

    Type

    Argument

    Description

    pnsCredentialName

    object

    <optional>

    The pns credentials to use.

    headers

    object

    <optional>

    The wns headers to include.

    callback

    object

    <optional>

    A JSON object that specifies the success and error callback functions.

  • createTile*Registration(channel, tags, template, options, callback)
    Creates a new registration with a tile template. There is a method for each tile template in the tile template catalog, such as createTileSquarePeekImageAndText01Registration.

    Parameters

    Name

    Type

    Argument

    Description

    channel

    string

    The device channel URI.

    tags

    string | array

    A single tag or array of tags (comma-separated list, no spaces).

    template

    object

    The body template of the registration.

    Name

    Type

    Description

    text{1..n}

    string

    Value of the text element with ID specified, such as text1. The number of text* properties depends on the tile type.

    image{1..n}src

    string

    Value of the src attribute of the image element with id specified, such as image1. The number of image* properties depends on the tile type.

    image{1..n}alt

    string

    Value of the alt attribute of the image element with id specified, such as image1. The number of image* properties depends on the tile type.

    lang

    string

    Value of the language of the binding element.

    options

    object

    <optional>

    The request options or callback.

    Name

    Type

    Argument

    Description

    headers

    object

    <optional>

    The wns headers to include.

    callback

    object

    <optional>

    A JSON object that specifies the success and error callback functions.

  • createToast*Registration(channel, tags, template, options, callback)
    Creates a new registration with a toast template. There is a method for each toast template in the toast template catalog, such as createToastText01Registration.

    Parameters

    Name

    Type

    Argument

    Description

    channel

    string

    The device channel URI.

    tags

    string | array

    A single tag or array of tags (comma-separated list, no spaces).

    template

    object

    The body template of the registration.

    Name

    Type

    Description

    text{1..n}

    string

    Value of the text element with ID specified, such as text1. The number of text* properties depends on the toast type.

    image{1..n}src

    string

    Value of the src attribute of the image element with id specified, such as image1. The number of image* properties depends on the toast type.

    image{1..n}alt

    string

    Value of the alt attribute of the image element with id specified, such as image1. The number of image* properties depends on the toast type.

    lang

    string

    Value of the language of the binding element.

    options

    object

    <optional>

    The request options or callback.

    Name

    Type

    Argument

    Description

    headers

    object

    <optional>

    The wns headers to include.

    callback

    object

    <optional>

    A JSON object that specifies the success and error callback functions.

  • listRegistrationsByChannel(channel, optionsOrCallback, callback)
    Retrieves registrations by channel URI.

    Parameters

    Name

    Type

    Argument

    Description

    channel

    string

    The device channel URI.

    options

    object

    <optional>

    The request options or callback function. Additional properties are passed as headers.

    Name

    Type

    Argument

    Description

    top

    object

    <optional>

    Specifies the maximum number of registration to obtain from the call.

    skip

    object

    <optional>

    Specifies the number of registrations to skip in the call.

    callback

    object

    <optional>

    A JSON object that specifies the success and error callback functions.

  • send(tags, payload, type, optionsOrCallback, callback)
    Sends a custom WNS notification. Use this method if you want to have full control on the payload.

    Parameters

    Name

    Type

    Argument

    Description

    tags

    array | string

    A single tag, tag expression, or array of tags.

    payload

    string

    The XML payload for the message.

    type

    string

    The message type. Corresponds to header X-WNS-Type (options are: wns/raw, wns/tile and wns/toast).

    optionsOrCallback

    object

    <optional>

    The request options or callback.

    Name

    Type

    Argument

    Description

    headers

    object

    <optional>

    The wns headers to include.

    callback

    object

    A JSON object that specifies the success and error callback functions.

  • sendBadge(tags, value, optionsOrCallback, callback)
    Sends a wns/badge WNS notification.

    Parameters

    Name

    Type

    Argument

    Description

    tags

    array | string

    Comma-separated list or array of tag identifiers.

    value

    object | string

    Either a numeric value or a string value that specifies a predefined badge glyph. Numerically, this value can accept any valid integer. A value of 0 clears the badge, values from 1-99 display as given, and any value greater than 99 displays as 99+. For details, see the badge element topic.

    optionsOrCallback

    object

    <optional>

    The request options or callback.

    Name

    Type

    Argument

    Description

    headers

    object

    <optional>

    The wns headers to include.

    callback

    object

    A JSON object that specifies the success and error callback functions.

  • sendRaw(tags, payload, optionsOrCallback, callback)
    Sends a wns/raw WNS notification.

    Parameters

    Name

    Type

    Argument

    Description

    tags

    array | string

    Comma-separated list or array of tag identifiers.

    payload

    object | string

    The raw payload.

    optionsOrCallback

    object

    <optional>

    The request options or callback.

    Name

    Type

    Argument

    Description

    headers

    object

    <optional>

    The wns headers to include.

    callback

    object

    A JSON object that specifies the success and error callback functions.

  • sendTile*(tags, payload, options, callback)
    Sends a Tile notification. There is a method for each tile template in the tile template catalog, such as sendTileSquarePeekImageAndText01.

    Parameters

    Name

    Type

    Argument

    Description

    tags

    string | array

    A single tag or tag expression.

    payload

    object

    The message payload. Multiple payload parameters can be passed.

    Name

    Type

    Description

    text{1..n}

    string

    Value of the text element with ID specified, such as text1. The number of text* properties depends on the tile type.

    image{1..n}src

    string

    Value of the src attribute of the image element with id specified, such as image1. The number of image* properties depends on the tile type.

    image{1..n}alt

    string

    Value of the alt attribute of the image element with id specified, such as image1. The number of image* properties depends on the tile type.

    lang

    string

    Value of the language of the binding element.

    type

    string

    Used in the general sendTile method.

    options

    object

    <optional>

    The request options or callback.

    Name

    Type

    Argument

    Description

    headers

    object

    <optional>

    The wns headers to include.

    callback

    object

    <optional>

    A JSON object that specifies the success and error callback functions.

  • sendToast*(tags, payload, options, callback)
    Sends a toast notification. There is a method for each toast template in the toast template catalog, such as sendToastText01.

    Parameters

    Name

    Type

    Argument

    Description

    tags

    string | array

    A single tag or tag expression.

    payload

    object

    The message payload.

    Name

    Type

    Description

    text{1..n}

    string

    Value of the text element with ID specified, such as text1. The number of text* properties depends on the toast type.

    image{1..n}src

    string

    Value of the src attribute of the image element with id specified, such as image1. The number of image* properties depends on the toast type.

    image{1..n}alt

    string

    Value of the alt attribute of the image element with id specified, such as image1. The number of image* properties depends on the toast type.

    lang

    string

    Value of the language of the binding element.

    options

    object

    <optional>

    The request options or callback.

    Name

    Type

    Argument

    Description

    headers

    object

    <optional>

    The wns headers to include.

    callback

    object

    <optional>

    A JSON object that specifies the success and error callback functions.

  • updatesRawTemplateRegistration(registrationId, channel, tags, template, optionsOrCallback, callback)
    Updates an existing raw template registration.

    Parameters

    Name

    Type

    Argument

    Description

    registrationId

    string

    The registration identifier.

    channel

    string

    The device channel URI.

    tags

    string | array

    The tags for the registration.

    template

    string | array

    The raw template for the registration.

    options

    object

    <optional>

    The request options or callback.

    Name

    Type

    Argument

    Description

    pnsCredentialName

    object

    <optional>

    The pns credentials to use.

    headers

    object

    <optional>

    The wns headers to include.

    etag

    object

    <optional>

    The etag to include.

    callback

    object

    A JSON object that specifies the success and error callback functions.

  • updateTile*Registration(registrationId, channel, tags, template, options, callback)
    Updates an existing template registration with a new tile template. There is a method for each tile template in the tile template catalog, such as updadteTileSquarePeekImageAndText01Registration.

    Parameters

    Name

    Type

    Argument

    Description

    registrationId

    string

    The registration identifier.

    channel

    string

    The device channel URI.

    tags

    string | array

    The tags for the registration.

    template

    object

    The body template of the registration.

    Name

    Type

    Description

    text{1..n}

    string

    Value of the text element with ID specified, such as text1. The number of text* properties depends on the tile type.

    image{1..n}src

    string

    Value of the src attribute of the image element with id specified, such as image1. The number of image* properties depends on the tile type.

    image{1..n}alt

    string

    Value of the alt attribute of the image element with id specified, such as image1. The number of image* properties depends on the tile type.

    lang

    string

    Value of the language of the binding element.

    options

    object

    <optional>

    The request options or callback.

    Name

    Type

    Argument

    Description

    headers

    object

    <optional>

    The wns headers to include.

    callback

    object

    A JSON object that specifies the success and error callback functions.

  • updateToast*Registration(registrationId, channel, tags, template, options, callback)
    Updates an existing template registration with a new toast template. There is a method for each toast template in the toast template catalog, such as updateToastText01Registration.

    Parameters

    Name

    Type

    Argument

    Description

    registrationId

    string

    The registration identifier.

    channel

    string

    The device channel URI.

    tags

    string | array

    The tags for the registration.

    template

    object

    The body template of the registration.

    Name

    Type

    Description

    text{1..n}

    string

    Value of the text element with ID specified, such as text1. The number of text* properties depends on the toast type.

    image{1..n}src

    string

    Value of the src attribute of the image element with id specified, such as image1. The number of image* properties depends on the toast type.

    image{1..n}alt

    string

    Value of the alt attribute of the image element with id specified, such as image1. The number of image* properties depends on the toast type.

    lang

    string

    Value of the language of the binding element.

    options

    object

    <optional>

    The request options or callback.

    Name

    Type

    Argument

    Description

    headers

    object

    <optional>

    The wns headers to include.

    callback

    object

    A JSON object that specifies the success and error callback functions.

Remarks

For an example of how to send push notifications from your mobile service to a Windows app, see Add push notifications to your Mobile Services app (Windows Store C#/Windows Store JavaScript/universal Windows C#/universal Windows JavaScript).

All methods return undefined.

The payload object has the following considerations with respect to naming and order of fields based on the specific template:

  • textN specifies the string value of the Nth text field in document order, starting from 1.

  • imageNsrc specifies the URL of the Nth image in document order, starting from 1.

  • imageNalt specifies the alt text of the Nth image in document order, starting from 1.

  • Any fields that are missing are assumed to be empty strings.

  • Any extra fields not required by a particular template are ignored.

When you call the sendRaw method, the notification payload is delivered to the app and not displayed in the UI. For more information, see Raw notification overview (Windows Store apps).

Example

The following script uses the send function on the wns object to send a toast notification to a Windows Store app.

function insert(item, user, request) {
// Define a payload for the Windows Store toast notification.
var payload = '<?xml version="1.0" encoding="utf-8"?><toast><visual>' +    
    '<binding template="ToastText01">  <text id="1">' +
    item.text + '</text></binding></visual></toast>';

request.execute({
    success: function() {
        // If the insert succeeds, send a notification.
        push.wns.send(null, payload, 'wns/toast', {
            success: function(pushResponse) {
                console.log("Sent push:", pushResponse);
                request.respond();
                },              
                error: function (pushResponse) {
                    console.log("Error Sending push:", pushResponse);
                    request.respond(500, { error: pushResponse });
                    }
                });
            }
        });
}

Example

The following script calls the sendRaw method of the wns object to send a pre-formatted toast notification by using the ToastImageAndText03 template.

function insert(item, user, request) {
    request.execute({
        success: function() {
            // Write to the response and then send a raw notification in the background.
            request.respond();
            push.wns.sendRaw(null,'{ notification: raw, text: "Raw notification string" }' + 
            item.text, {
                success: function(pushResponse) {
                    console.log("Sent push:", pushResponse);
                }
            });
        }
    });
}

See Also

Mobile Services JavaScript (Node.js) backend library
apns object
gcm object
mpns object
wns object