Skip to content

Files

Latest commit

04163ef · Apr 15, 2015

History

History
101 lines (73 loc) · 4.92 KB

mobile-services-javascript-backend-windows-store-javascript-push-notifications-app-users.md

File metadata and controls

101 lines (73 loc) · 4.92 KB

Send push notifications to authenticated users

[AZURE.INCLUDE mobile-services-selector-push-users]

This topic shows you how to send push notifications to an authenticate user on any registered device. Unlike the previous push notification tutorial, this tutorial changes your mobile service to require that a user be authenticated before the client can register with the notification hub for push notifications. Registration is also modified to add a tag based on the assigned user ID. Finally, the server script is updated to send the notification only to the authenticated user instead of to all registrations.

This tutorial walks you through the following process:

  1. Updating the service to require authentication for registration
  2. Updating the app to log in before registration
  3. Testing the app

This tutorial supports both Windows Store and Windows Phone Store apps.

##Prerequisites

Before you start this tutorial, you must have already completed these Mobile Services tutorials:

After you have completed both tutorials, you can prevent unauthenticated users from registering for push notifications from your mobile service.

##Update the service to require authentication to register

[AZURE.INCLUDE mobile-services-javascript-backend-push-notifications-app-users]

  1. Replace the insert function with the following code, then click Save:

    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>';
    
    // Get the ID of the logged-in user.
    var userId = user.userId;		
    
    request.execute({
        success: function() {
            // If the insert succeeds, send a notification to all devices 
        	// registered to the logged-in user as a tag.
            	push.wns.send(userId, 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 });
                        }
                    });
                }
            });
    

    }

    This insert script uses the user ID tag to send a push notification (with the text of the inserted item) to all Windows Store app registrations created by the logged-in user.

##Update the app to log in before registration

[AZURE.INCLUDE mobile-services-windows-store-javascript-push-notifications-app-users]

##Test the app

[AZURE.INCLUDE mobile-services-windows-test-push-users]