Exporteren (0) Afdrukken
Alles uitvouwen
EN
Deze inhoud is niet beschikbaar in uw taal, maar wel in het Engels.

wns object

Updated: March 8, 2012

Exposes the functionality of the Windows Notification Service (WNS) in Windows Azure Mobile Services server scripts.

This object contains the following methods and sets of methods that are used to send notifications:

sendTile*

Sends a tile notification to a specific channel, where each version of the sendTile* method uses a different WNS tile template.

Syntax

Parameters

 

Parameter Description

channel

Required channel URI value or array of URI values that indicates the channels to receive the notification.

payload

Required object that contains the notification information, where the fields required are based on the toast template of the specific notification. For example, when calling the sendTileWideImageAndText02 method, you supply a payload object with the following fields:

{
    text1: 'text field 1',
    text2: 'text field 2',
    image1src: 'image1.png',
    image1alt: 'alt text'
}

See the remarks for information on naming and ordering of fields in the payload object.

options

Object that is used to supply the success and error callbacks and other optional behaviors of the notification:

{
    success: success,
    error: error,
    headers: wns_headers 
}

Where success is the function that is called after the notification is sent, error is the function that is called when an error occurs, and headers is the list of any additional HTTP request headers that are to be included in the request sent to WNS.

For a list of available HTTP request headers, see Push notification service request and response headers.

Returns

Undefined.

sendToast*

Sends a toast notification to a specific channel, where each version of the sendToast* method uses a different toast template.

Syntax

wns.sendToast*(channel, payload, options)

The asterisk (*) is a placeholder for one of the tile templates, shown in the following:

Parameters

 

Parameter Description

channel

Required channel URI value or array of URI values that indicates the channels to receive the notification.

payload

Required object that contains the notification information, where the fields required are based on the toast template of the specific notification. For example, when calling the sendToastImageAndText03 method, you supply a payload object with the following fields:

{
    text1: 'headline text',
    text2: 'body text',
    image1src: 'image1.png',
    image1alt: 'alt text'
}

See the remarks for information on naming and ordering of fields in the payload object.

options

Object that is used to supply the success and error callbacks and other optional behaviors of the notification:

{
    success: success,
    error: error,
    headers: wns_headers, 
    launch: launch_uri,
    duration: duration_time 
}

Where success is the function that is called after the notification is sent, error is the function that is called when an error occurs, headers is the list of any additional HTTP request headers that are to be included in the request sent to WNS, launch_uri is an optional URI to navigate to when the toast notification is raised, and duration_time is the length of time that the toast notification is displayed.

For a list of available HTTP request headers, see Push notification service request and response headers.

Valid values for the duration_time field are long and short.

Returns

Undefined.

sendBadge

Sends a badge notification to a specific channel.

Syntax

wns.sendBadge(channel, value, options)

Parameters

 

Parameter Description

channel

Required channel URI value or array of URI values that indicates the channels to receive the notification.

value

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+. Supported string values include the following

  • none

  • activity

  • alert

  • available

  • away

  • busy

  • newMessage

  • paused

  • playing

  • unavailable

  • error

  • attention

For more information, see the Badge schema.

options

Object that is used to supply the success and error callbacks and other optional behaviors of the notification:

{
    success: success,
    error: error,
    headers: wns_headers, 
}

Where success is the function that is called after the notification is sent, error is the function that is called when an error occurs, and headers is the list of any additional HTTP request headers that are to be included in the request sent to WNS.

For a list of available HTTP request headers, see Push notification service request and response headers.

Returns

Undefined.

sendRaw

Sends a raw notification to a specific channel.

Syntax

wns.sendRaw(channel, value, options)

Parameters

 

Parameter Description

channel

Required channel URI value or array of URI values that indicates the channels to receive the notification.

value

An application-specific string that is delivered to the client without modification.

options

Object that is used to supply the success and error callbacks and other optional behaviors of the notification:

{
    success: success,
    error: error,
    headers: wns_headers, 
}

Where success is the function that is called after the notification is sent, error is the function that is called when an error occurs, and headers is the list of any additional HTTP request headers that are to be included in the request sent to WNS.

For a list of available HTTP request headers, see Push notification service request and response headers.

Returns

Undefined.

send

Sends a pre-formatted notification to a specific channel.

ImportantImportant
To use this method, you must know the specific payload format of your selected notification type.

Syntax

wns.send(channel, payload, type, value, options)

Parameters

 

Parameter Description

channel

Required channel URI value or array of URI values that indicates the channels to receive the notification.

payload

An application-specific string that is delivered to the client without modification.

Review the push notification topics in the Windows Store app documentation for information about payload formats. For more information, see The tile template catalog, The toast template catalog, and Badge overview.

type

Specifies the type of the notification, as one of the following string values:

 

Value Description

wns/badge

A notification to create a badge overlay on the tile. The Content-Type header included in the notification request must be set to "text/xml".

wns/tile

A notification to update the tile content. The Content-Type header included in the notification request must be set to "text/xml".

wns/toast

A notification to raise a toast on the client. The Content-Type header included in the notification request must be set to "text/xml".

wns/raw

A notification which can contain a custom payload and is delivered directly to the app. The Content-Type header included in the notification request must be set to "application/octet-stream".

options

Object that is used to supply the success and error callbacks and other optional behaviors of the notification:

{
    success: success,
    error: error,
    headers: wns_headers, 
}

Where success is the function that is called after the notification is sent, error is the function that is called when an error occurs, and headers is the list of any additional HTTP request headers that are to be included in the request sent to WNS.

For a list of available HTTP request headers, see Push notification service request and response headers.

Returns

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).

The following script uses the wns object to send toast notifications to a Windows Store app.

function insert(comment, user, request) { 
  request.execute({ 
    success: function() { 
      var sql = "SELECT deviceId FROM devices " + 
      "INNER JOIN posts ON devices.userId = posts.userId " + 
      "WHERE posts._id = ?"; 
      mssql.query(sql, [comment.postId], { 
        success: function(results) { 
          if(results.length > 0) { 
            push.wns.sendToastText04(results[0].deviceId, { 
              text1: comment.username + ' commented on your post!' 
            }); 
          } 
        } 
      }); 
      // Write to the response for the original request while the push notification happens in the background 
      request.respond(); 
    } 
  }); 
}

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(item.channel,'{ notification: raw, text: "Raw notification string" }' + 
            item.text, {
                success: function(pushResponse) {
                    console.log("Sent push:", pushResponse);
                }
            });
        }
    });
}

The following script calls the send 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 the pre-formatted notification in the background.
            request.respond();
            push.wns.send(item.channel,'<toast launch=""><visual lang="en-US">' +
            '<binding template="ToastImageAndText03"><image id="1" src="imageUri" />' + 
            '<text id="1">Template: ToastImageAndText03</text>' + 
            '<text id="2">' + item.text + '</text></binding></visual></toast>',
            "wns/toast", {
                success: function(pushResponse) {
                    console.log("Sent push:", pushResponse);
                }
            });
        }
    });
}

Weergeven:
© 2014 Microsoft