This topic shows you how to send push notifications to an authenticate user on any registered iOS device. Unlike the previouspush notification tutorial, this tutorial changes your mobile service to require that a user be authenticated before the iOS 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:
- Updating the service to require authentication for registration
- Updating the app to log in before registration
- Testing the app
Prerequisites
Before you start this tutorial, you must have already completed these Mobile Services tutorials:
- Get started with authentication
Adds a login requirement to the TodoList sample app. - Get started with push notifications
Configures the TodoList sample app for push notifications by using Notification Hubs.
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
- Log on to the Azure Management Portal, click Mobile Services, and then click your mobile service.
- Click the Push tab, select Only Authenticated Users for Permissions, then click Edit Script.This allows you to customize the push notification registration callback function. If you use Git to edit your source code, this same registration function is found in
.\service\extensions\push.js
. - Replace the existing register function with the following code:
exports.register = function (registration, registrationContext, done) { // Get the ID of the logged-in user. var userId = registrationContext.user.userId; // Perform a check here for any disallowed tags. if (!validateTags(registration)) { // Return a service error when the client tries // to set a user ID tag, which is not allowed. done("You cannot supply a tag that is a user ID"); } else{ // Add a new tag that is the user ID. registration.tags.push(userId); // Complete the callback as normal. done(); } }; function validateTags(registration){ for(var i = 0; i < registration.tags.length; i++) { console.log(registration.tags[i]); if (registration.tags[i] .search(/facebook:|twitter:|google:|microsoft:/i) !== -1){ return false; } return true; } }
This adds a tag to the registration that is the ID of the logged-in user. The supplied tags are validated to prevent a user from registering for another user's ID. When a notification is sent to this user, it is received on this and any other device registered by the user. - Click the back arrow, click the Data tab, click TodoItem, click Script and select Insert.
- Replace the insert function with the following code, then click Save:
function insert(item, user, request) { function insert(item, user, request) { request.execute(); setTimeout(function() { push.apns.send(null, { alert: "Alert: " + item.text, payload: { "Hey, a new item arrived: '" + item.text + "'" } }); }, 2500); } }
This insert script uses the user ID tag to send a push notification (with the text of the inserted item) to all Windows Phone (MPNS) app registrations created by the logged-in user.
Update the app to log in before registration
Next, you need to change the way that push notifications are registered to make sure that the user is authenticated before registration is attempted.
- In QSAppDelegate.m, remove the implementation of didFinishLaunchingWithOptions altogether:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions { // Register for remote notifications [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound]; return YES; }
- Open the project file QSTodoListViewController.m and in the viewDidLoad method, add the code removed above to the add:
- (void)viewDidAppear:(BOOL)animated { MSClient *client = self.todoService.client; if (client.currentUser != nil) { return; } [client loginWithProvider:@"facebook" controller:self animated:YES completion:^(MSUser *user, NSError *error) { [self refresh]; }]; // Register for remote notifications [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound]; }
Test the app
- Press the Run button to build the project and start the app in an iOS capable device, then click OK to accept push notifications. Verify that the log in and authentication succeeds.
- In the app, type meaningful text, such as A new Mobile Services task and then click the plus (+) icon.
- Verify that a notification is received, then click OK to dismiss the notification.
- Repeat step 2 and immediately close the app, then verify that the following push is shown.
You have successfully completed this tutorial.
Source from
http://azure.microsoft.com/en-us/documentation/articles/mobile-services-javascript-backend-ios-push-notifications-app-users/
No comments :
Post a Comment