Windows Azure

How to Setup iOS Push Notifications on Windows Azure

In this post I’ll give a quick overview of how to add Push Notifications to the iOS application we created in our previous blog post entitled “How to Setup Windows Azure for iOS in under 5 minutes.” It will be helpful to use that sample app as a starting point for this tutorial.

On the Azure website, they have a detailed tutorials on how to get Push Notifications working for iOS; however, if you’re pretty comfortable in the Apple Developer center you can follow the simple steps outlined below.

There are three main areas that need to be configured: the Apple Developer Center, Azure Management Portal, and your App in Xcode.

Apple Developer Center Configuration

  1. Setup Push Notifications for your iOS app: in order to enable push notifications, you will need to go to the Apple Developer center and create an App ID and Provisioning profile with Push Notifications turned on. To do so, you’ll need to create/upload a certificate – save this certificate because in the next step you upload it to Azure.
    Push+Notifications+on+iOS-300x149 How to Setup iOS Push Notifications on Windows Azure

Azure Management Portal Configuration

  1. Login and go to your app
  2. Upload your Push Notification Certificate
    Azure+Mobile+Services-300x127 How to Setup iOS Push Notifications on Windows Azure
  3. Update the Insert script for the app
    Azure+Table+Push+Insert-300x149 How to Setup iOS Push Notifications on Windows Azure

    function insert(item, user, request) {
    request.execute();
    // Set timeout to delay the notification, to provide time for the
    // app to be closed on the device to demonstrate toast notifications
    setTimeout(function() {
    push.apns.send(item.deviceToken, {
    alert: "Toast: " + item.text,
    payload: {
    inAppMessage: "Hey, a new item arrived: '" + item.text + "'"
    }
    });
    }, 2500);
    }

App Configuration

  1. Open the app that we built in the previous post.
  2. Update the Bundle ID to match the new provisioning profile you created above
  3. Open QSAppDelegate.h and add the following property:
    <a href="http://twitter.com/#!/@property">@property</a>  (strong, nonatomic) NSString *deviceToken;
    
  4. Open QSAppDelegate.m and replace the following method
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
    (NSDictionary *)launchOptions
    {
        // Register for remote notifications
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
        UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
        return YES;
    }
    
  5. Then add the following methods to QAAppDelegate.m
    // We are registered, so now store the device token (as a string) on the AppDelegate instance
    // taking care to remove the angle brackets first.
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:
    (NSData *)deviceToken {
        NSCharacterSet *angleBrackets = [NSCharacterSet characterSetWithCharactersInString:@"&lt;&gt;"];
        self.deviceToken = [[deviceToken description] stringByTrimmingCharactersInSet:angleBrackets];
    }
    
    // Handle any failure to register. In this case we set the deviceToken to an empty
    // string to prevent the insert from failing.
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:
    (NSError *)error {
        NSLog(@"Failed to register for remote notifications: %@", error);
        self.deviceToken = @"";
    }
    
    // Because toast alerts don't work when the app is running, the app handles them.
    // This uses the userInfo in the payload to display a UIAlertView.
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:
    (NSDictionary *)userInfo {
        NSLog(@"%@", userInfo);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:
        [userInfo objectForKey:@"inAppMessage"] delegate:nil cancelButtonTitle:
        @"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    
  6. Open “QSTodoListViewController.m” and import the QAAppsDelegate header.
    <a href="http://www.windowsazure.com/en-us/develop/mobile/tutorials/get-started-with-push-ios/#">#import</a> "QSAppDelegate.h"
    
  7. Update the NSDictionary in the IBAction in “QSTodoListViewController.m” :
    // Get a reference to the AppDelegate to easily retrieve the deviceToken
    QSAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    
    NSDictionary *item = @{
        @"text" : itemText.text,
        @"complete" : @(NO),
        // add the device token property to our todo item payload
        @"deviceToken" : delegate.deviceToken
    };
    

How to Test iOS Push Notifications in Azure

  1. Create a new To Do item and you’ll receive a push notification almost immediately.
  2. To see the toast message, create a To Do item then immediately close the app and you’ll see the toast message.

The post How to Setup iOS Push Notifications on Windows Azure appeared first on OptimusMobility.