Skip to content

Latest commit

 

History

History
137 lines (103 loc) · 5.65 KB

app-service-mobile-xamarin-ios-get-started-push.md

File metadata and controls

137 lines (103 loc) · 5.65 KB
title description services documentationcenter author manager editor ms.assetid ms.service ms.workload ms.tgt_pltfrm ms.devlang ms.topic ms.date ms.author
Add push notifications to your Xamarin.iOS app with Azure App Service
Learn how to use Azure App Service to send push notifications to your Xamarin.iOS app
app-service\mobile
xamarin
ysxu
dwrede
2921214a-49f8-45e1-a306-a85ce21defca
app-service-mobile
mobile
mobile-xamarin-ios
dotnet
article
10/12/2016
yuaxu

Add push notifications to your Xamarin.iOS App

[!INCLUDE app-service-mobile-selector-get-started-push]

Overview

In this tutorial, you add push notifications to the Xamarin.iOS quick start project so that a push notification is sent to the device every time a record is inserted.

If you do not use the downloaded quick start server project, you will need the push notification extension package. See Work with the .NET backend server SDK for Azure Mobile Apps for more information.

Prerequisites

  • Complete the Xamarin.iOS quickstart tutorial.
  • A physical iOS device. Push notifications are not supported by the iOS simulator.

Register the app for push notifications on Apple's developer portal

[!INCLUDE Enable Apple Push Notifications]

Configure your Mobile App to send push notifications

[!INCLUDE app-service-mobile-apns-configure-push]

Update the server project to send push notifications

[!INCLUDE app-service-mobile-update-server-project-for-push-template]

Configure your Xamarin.iOS project

[!INCLUDE app-service-mobile-xamarin-ios-configure-project]

Add push notifications to your app

  1. In QSTodoService, add the following property so that AppDelegate can acquire the mobile client:

         public MobileServiceClient GetClient {
         get
         {
             return client;
         }
         private set
         {
             client = value;
         }
     }
    
  2. Add the following using statement to the top of the AppDelegate.cs file.

     using Microsoft.WindowsAzure.MobileServices;
     using Newtonsoft.Json.Linq;
    
  3. In AppDelegate, override the FinishedLaunching event:

     public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
     {
         // registers for push for iOS8
         var settings = UIUserNotificationSettings.GetSettingsForTypes(
             UIUserNotificationType.Alert
             | UIUserNotificationType.Badge
             | UIUserNotificationType.Sound,
             new NSSet());
    
         UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
         UIApplication.SharedApplication.RegisterForRemoteNotifications();
    
         return true;
     }
    
  4. In the same file, override the RegisteredForRemoteNotifications event. In this code you are registering for a simple template notification that will be sent across all supported platforms by the server.

    For more information on templates with Notification Hubs, see Templates.

     public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
     {
         MobileServiceClient client = QSTodoService.DefaultService.GetClient;
    
         const string templateBodyAPNS = "{\"aps\":{\"alert\":\"$(messageParam)\"}}";
    
         JObject templates = new JObject();
         templates["genericMessage"] = new JObject
         {
             {"body", templateBodyAPNS}
         };
    
         // Register for push with your mobile app
         var push = client.GetPush();
         push.RegisterAsync(deviceToken, templates);
     }
    
  5. Then, override the DidReceivedRemoteNotification event:

     public override void DidReceiveRemoteNotification (UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
     {
         NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
    
         string alert = string.Empty;
         if (aps.ContainsKey(new NSString("alert")))
             alert = (aps [new NSString("alert")] as NSString).ToString();
    
         //show alert
         if (!string.IsNullOrEmpty(alert))
         {
             UIAlertView avAlert = new UIAlertView("Notification", alert, null, "OK", null);
             avAlert.Show();
         }
     }
    

Your app is now updated to support push notifications.

Test push notifications in your app

  1. Press the Run button to build the project and start the app in an iOS capable device, then click OK to accept push notifications.

    [!NOTE] You must explicitly accept push notifications from your app. This request only occurs the first time that the app runs.

  2. In the app, type a task, and then click the plus (+) icon.

  3. Verify that a notification is received, then click OK to dismiss the notification.

  4. Repeat step 2 and immediately close the app, then verify that a notification is shown.

You have successfully completed this tutorial.