Skip to content
Erika Ehrli edited this page Aug 1, 2016 · 2 revisions

Send and Receive Push Notifications on iOS, Android, and Windows

Overview

Azure Notification Hubs provide an easy-to-use infrastructure that enables you to send mobile push notifications from any backend (in the cloud or on-premises) to all major platforms, including—iOS, Android, and Windows.

With Notification Hubs you can easily send cross-platform, personalized push notifications, abstracting the details of the different platform notification systems (PNSs). With a single API call, you can target individual users or entire audience segments containing millions of users, across all their devices at the same time.

Push notifications drive better engagement with your app users. Send breaking news, enterprise-wide announcements, and location-based coupons.

This tutorial shows you how to use Azure Notification Hubs to send and receive push notifications in the Shopping Demo App. The app sends and receives notifications when items are sold.

Prerequisites

Sign up for Microsoft Azure

You need an Azure account to complete this tutorial. You can:

If you want to get started with Azure App Service before you sign up for an Azure account, go to Try App Service. There, you can immediately create a short-lived starter mobile app in App Service—no credit card required, and no commitments.

Set up the development environment

To start, set up your development environment by installing the latest version of the Azure SDK and Xamarin

If you do not have Visual Studio installed, use the link for Visual Studio 2015, and Visual Studio will be installed along with the SDK.

Setting up Push Notifications in server side

Setting up the Notification Hub

The first step is creating a Notification Hub in your Azure account. Setup may vary by platform. Please see the documentation below for setup by platform:

Once you configure the Notification Hub by platform, you can send debug push notifications manually. You will need to define a trigger in your API to send them.

Sending pushes from the Mobile App

Shopping Demo App client apps consume a Mobile App API to interact with the data. When you tap to buy an item, the app calls the API that triggers a Push Notification.

Invoke the API with the item to buy:

public async Task BuySaleItemAsync(SaleItem item)
{
    try
    {
        bool buySucceeded = await this.MobileService.InvokeApiAsync<SaleItem, bool>("buy", item);

        if (buySucceeded)
        {
            await UserDialogs.Instance.AlertAsync("Thanks for buying this item");
        }
    }
}

At this point, the app can send push notifications, but you still need to connect the client apps to the app Notification Hub.

Setting up Push Notifications in client side

The Push Notifications scenario happens in the client app and delivers a single push when you tap to buy an item. In order to make the demo self-sufficient, the push is sent to every device, although it should be sent to the owner of the item in a real scenario.

The subscription is made in a similar way in each platform thanks to the client SDKs, although differ a little bit to fit to each platform.

Xamarin.Android

In Android the subscription happens in MainActivity.cs and AndroidPushService.cs:

await AndroidPushService.Register(null);

Xamarin.iOS

In iOS, the entry point AppDelegate.cs is the one in charge of managing the subscription, or registration as Apple calls this part.

Windows 10 Mobile

MainPage.xaml.cs takes control on the subscription:

private async Task InitNotificationsAsync()
{
     // Get a channel URI from WNS.
     var channel = await Windows.Networking.PushNotifications.PushNotificationChannelManager
         .CreatePushNotificationChannelForApplicationAsync();

     await SaleItemDataService.Instance.MobileService.GetPush().RegisterAsync(channel.Uri);
}

Learn more

For more information, please visit http://azure.com/xamarin