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 |
[!INCLUDE app-service-mobile-selector-get-started-push]
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.
- Complete the Xamarin.iOS quickstart tutorial.
- A physical iOS device. Push notifications are not supported by the iOS simulator.
[!INCLUDE Enable Apple Push Notifications]
[!INCLUDE app-service-mobile-apns-configure-push]
[!INCLUDE app-service-mobile-update-server-project-for-push-template]
[!INCLUDE app-service-mobile-xamarin-ios-configure-project]
-
In QSTodoService, add the following property so that AppDelegate can acquire the mobile client:
public MobileServiceClient GetClient { get { return client; } private set { client = value; } }
-
Add the following
using
statement to the top of the AppDelegate.cs file.using Microsoft.WindowsAzure.MobileServices; using Newtonsoft.Json.Linq;
-
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; }
-
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); }
-
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.
-
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.
-
In the app, type a 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 a notification is shown.
You have successfully completed this tutorial.