The AWS Mobile SDK for Xamarin is now included in the AWS SDK for .NET. This guide references the archived version of the Mobile SDK for Xamarin.
Receive Push Notifications using SNS (Xamarin iOS)
This document explains how to send push notifications to a Xamarin iOS application using Amazon Simple Notification Service (SNS) and the AWS Mobile SDK for .NET and Xamarin.
Project Setup
Prerequisites
You must complete all of the instructions on the Setting Up the AWS Mobile SDK for .NET and Xamarin before beginning this tutorial.
Set Permissions for SNS
Follow Step 2 in Setting Up the AWS Mobile SDK for .NET and Xamarin to attach the policy mentioned below to your application’s roles. This will give your application the proper permissions to access SNS:
-
Go to the IAM Console
and select the IAM role that you want to configure. -
Click Attach Policy, select the AmazonSNSFullAccess policy and click Attach Policy.
Warning
Using AmazonSNSFullAccess is not recommended in a production environment. We use it here to allow you to get up and running quickly. For more information about specifying permissions for an IAM role, see Overview of IAM Role Permissions.
Obtain Membership in the Apple iOS Developer Program
You will need to run your app on a physical device to receive push notifications. To
run your app on a device, you must have a membership in the Apple iOS Developer Program
Membership
Create an iOS Certificate
First, you need to create an iOS Certificate. Then, you need to create a provisioning profile configured for push notifications. To do so:
-
Go to the Apple Developer Member Center
, click Certificates, Identifiers & Profiles. -
Click Identifiers under iOS Apps, click the plus button in the upper right-hand corner of the web page to add a new iOS App ID, and enter an App ID description.
-
Scroll down to the Add ID Suffix section and select Explicit App ID and enter your bundle identifier.
-
Scroll down to the App Services section and select Push Notifications.
-
Click Continue.
-
Click Submit.
-
Click Done.
-
Select the App ID you just created and then click Edit.
-
Scroll down to the Push Notifications section. Click Create Certificate under Development SSL Certificate.
-
Follow the instructions to create a Certificate Signing Request (CSR), upload the request, and download an SSL certificate that will be used to communicate with Apple Notification Service (APNS).
-
Return to the Certificates, Identifiers & Profiles page. Click All under Provisioning Profiles.
-
Click the plus button in the upper right-hand corner to add a new provisioning profile.
-
Select iOS App Development, and then click Continue.
-
Select your App ID, and then click Continue.
-
Select your developer certificate, and then click Continue.
-
Select your device, and then click Continue.
-
Enter a profile name, and then click Generate.
-
Download and double-click the provision file to install the provisioning profile.
For more information about provisioning a profile configured for push notifications,
see Apple’s Configuring Push Notifications
Use Certificate to Create Platform ARN in SNS Console
-
Run the KeyChain access app, select My Certificates on the lower left-hand side of the screen, and then right-click the SSL certificate you generated to connect to APNS and select Export. You will be prompted to specify a name for the file and a password to protect the certificate. The certificate will be saved in a P12 file.
-
Go to the SNS Console
and click Applications on the left-hand side of the screen. -
Click Create platform application to create a new SNS platform application.
-
Enter an Application Name.
-
Select Apple Development for Push notification platform.
-
Click Choose File and select the P12 file you created when you exported your SSL certificate.
-
Enter the password you specified when you exported the SSL certificate and click Load Credentials From File.
-
Click Create platform application.
-
Select the Platform Application you just created and copy the Application ARN. You will need this in the upcoming steps.
Add NuGet Package for SNS to Your Project
Follow Step 4 of the instructions in Setting Up the AWS Mobile SDK for .NET and Xamarin to add the Amazon Simple Notification Service NuGet package to your project.
Create an SNS Client
var snsClient = new AmazonSimpleNotificationServiceClient(credentials, region);
Register Your Application for Remote Notifications
To register an application, call RegisterForRemoteNotifications on your UIApplication object, as shown below. Place the following code in AppDelegate.cs, inserting your platform application ARN where prompted below:
public override bool FinishedLaunching(UIApplication app, NSDictionary options) { // do something var pushSettings = UIUserNotificationSettings.GetSettingsForTypes ( UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, null ); app.RegisterUserNotifications(pushSettings); app.RegisterForRemoteNotifications(); // do something return true; } public override void RegisteredForRemoteNotifications(UIApplication application, NSData token) { var deviceToken = token.Description.Replace("<", "").Replace(">", "").Replace(" ", ""); if (!string.IsNullOrEmpty(deviceToken)) { //register with SNS to create an endpoint ARN var response = await SnsClient.CreatePlatformEndpointAsync( new CreatePlatformEndpointRequest { Token = deviceToken, PlatformApplicationArn = "YourPlatformArn" /* insert your platform application ARN here */ }); } }
Send a Message from the SNS Console to Your Endpoint
-
Go to the SNS Console > Applications
. -
Select your platform application, select an endpoint, and click Publish to endpoint.
-
Type in a text message in the text box and click Publish message to publish a message.