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

Send push notification

Updated: April 15, 2014

In a JavaScript backend mobile service, server scripts that are registered to table operations and scheduled jobs use the global push object to send push notifications to your apps. When sending push notifications from a custom API, the push object is obtained from the service property of the supplied request object, as in the following code that handles a POST request:

exports.post = function(request, response) {
    var push = request.service.push;
    // Send a push request to one or more devices, 
    // and then return a 200 status code if no error occurs.
};

To send push notifications to a device, you must configure your mobile service to work with the platform-specific push notification provider. When using Notification Hubs integration, you must also register your clients with the notification hub. For more information, see Get started with push notifications (Windows Store C# / Windows Store JavaScript / Windows Phone / iOS / Android).

Windows Store: The following script sends a push as a toast notification to a Windows Store app app.

function insert(comment, user, request) {
    request.execute({
        success: function() {
            var sql = "SELECT channel 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].channel, {
                            text1: comment.username + ' commented on your post!'
                        }, {
                            success: function(pushResponse) {
                                console.log("Sent push:", pushResponse);
                            }
                        });
                    }
                }
            });
            // Write to the response for the original request 
            // while the push notification happens in the background.
            request.respond();
        }
    });
}

Windows Phone: The following script sends the same push as a flip tile notification to a Windows Phone 8 app.

function insert(comment, user, request) {
    request.execute({
        success: function() {
            var sql = "SELECT channel 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.mpns.sendFlipTile(results[0].channel, {
                            title: comment.username + ' commented on your post!'
                        }, {
                            success: function(pushResponse) {
                                console.log("Sent push:", pushResponse);
                            }
                        });
                    }
                }
            });
            // Write to the response for the original request 
            // while the push notification happens in the background.
            request.respond();
        }
    });
}

iOS: The following script sends a text notification to a specific device when a new item is inserted into a table.

function insert(item, user, request) {
    request.execute({
        success: function() {
            // Write to the response and then send the push notification.
            request.respond();
            push.apns.send(item.deviceToken, {
                payload: { itemText: item.text, userId: user.userId } 
            });
        }
    });
}

Android: The following script sends a text notification to a specific device when a new item is inserted into a table.

function insert(item, user, request) {
    request.execute({
        success: function() {
            // Write to the response and then send the push notification.
            request.respond();
            push.gcm.send(item.channel, item.text, {
                   success: function(response) {
                       console.log('Push notification sent: ', response);
                }, error: function(error) {
                       console.log('Error sending push notification: ', error);
                }
            });
        }
    });
}

For more information, see the Mobile Services script reference

Weergeven:
© 2014 Microsoft