wns object
The feature described in this topic is available only in preview. To use this feature and other new Windows Azure capabilities, sign up for the free preview.
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
wns.sendTile*(channel, payload, options)
The asterisk (*) is a placeholder for one of the tile templates, shown in the following:
-
sendTileSquareBlock
-
sendTileSquareText01
-
sendTileSquareText02
-
sendTileSquareText03
-
sendTileSquareText04
-
sendTileWideText01
-
sendTileWideText02
-
sendTileWideText03
-
sendTileWideText04
-
sendTileWideText05
-
sendTileWideText06
-
sendTileWideText07
-
sendTileWideText08
-
sendTileWideText09
-
sendTileWideText10
-
sendTileWideText11
-
sendTileSquareImage
-
sendTileSquarePeekImageAndText01
-
sendTileSquarePeekImageAndText02
-
sendTileSquarePeekImageAndText03
-
sendTileSquarePeekImageAndText04
-
sendTileWideImage
-
sendTileWideImageCollection
-
sendTileWideImageAndText01
-
sendTileWideImageAndText02
-
sendTileWideBlockAndText01
-
sendTileWideBlockAndText02
-
sendTileWideSmallImageAndText01
-
sendTileWideSmallImageAndText02
-
sendTileWideSmallImageAndText03
-
sendTileWideSmallImageAndText04
-
sendTileWideSmallImageAndText05
-
sendTileWidePeekImageCollection01
-
sendTileWidePeekImageCollection02
-
sendTileWidePeekImageCollection03
-
sendTileWidePeekImageCollection04
-
sendTileWidePeekImageCollection05
-
sendTileWidePeekImageCollection06
-
sendTileWidePeekImageAndText01
-
sendTileWidePeekImageAndText02
-
sendTileWidePeekImage01
-
sendTileWidePeekImage02
-
sendTileWidePeekImage03
-
sendTileWidePeekImage04
-
sendTileWidePeekImage05
-
sendTileWidePeekImage06
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 |
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
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.
Important |
|---|
| 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:
|
||||||||||
|
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:
-
textNspecifies the string value of the Nth text field in document order, starting from 1. -
imageNsrcspecifies the URL of the Nth image in document order, starting from 1. -
imageNaltspecifies 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); } }); } }); }
Reference
apns objectgcm object
mpns object
wns object
Concepts
Mobile Services server script reference
Important