This is part 3 of a 4-part series about Azure App Service. You can find the other posts in this series here:

You can also learn more about Azure App Service from my Pluralsight course Introduction to Azure App Services. It is a bit older, but still very useful.
And if you like these posts and want to support me in what I do, please visit my Pluralsight page and follow me.

Mobile is one of the most used form factors for applications and the internet. Many websites are optimized for mobile use and there are millions of mobile apps available.

There are many ways to create mobile applications. You can for instance create them with Xamarin, Cordova, with the native languages for iOS, Android or Windows and with many more frameworks and languages.

And most mobile applications work with data. Usually, this data comes from a server and the mobile app connects to the server through the internet. This communication needs to be secured.

On top of that, many mobile applications need functionality like being able to work offline and sending push notifications.

Working with data, working offline and sending push notifications are difficult challenges that are complex to implement from scratch. Azure App Service Mobile Apps can help with these challenges as they offer these functionalities out of the box.

Azure App Service Mobile Apps are very similar to Web Apps and other App Service types. Mobile Apps have all the App Service shared features, like deployment slots and automatic scaling. And Mobile Apps has a unique feature: Offline Sync. Other features that help with mobile, like Push Notifications, are part of the shared features of App Service and can be used by all of the App Service types.

Even though Push Notifications is not unique to Mobile Apps, we’ll discuss it in this post, because it is relevant to working with mobile applications.

Mobile App facts

An Azure Mobile App is a mobile backend-as-a-service. Here are some of its properties:

  • Mobile Apps run APIs for your mobile applications
  • You can create APIs that run in Azure Mobile Apps in Node.js and in .NET
  • Mobile Apps come with an SLA (Service Level Agreement) that promises that they will stay up and running 99.95% of the time
  • You can use all of the App Service shared features with Mobile Apps, including custom domains, deployment slots, scaling and hybrid connections
  • The Azure Mobile Apps SDK that helps you to connect to Mobile Apps from your mobile application is available for:
    • iOS (Objective-C and Swift)
    • Android (Java)
    • Windows (C#)
    • Xamarin (including Forms)
    • Apache Cordova
  • Mobile Apps come with a unique feature that helps you in the development of mobile applications – Offline Sync

Offline sync

Mobile applications need to be able to work with intermittent internet connectivity. This is because the mobile device moves around, and the connection isn’t guaranteed. Imagine a user that uses her phone on a train. She goes through tunnels and areas with poor reception. Sometimes she’ll have a good internet connection, sometimes a very poor one and sometimes, no connection at all. The mobile application should be able to deal with that.

This is a difficult problem that mobile developers have been trying to solve for years with all sorts of technologies, including HTML5 offline storage.

Azure Mobile Apps can help. They offer the Offline Sync feature, which allows users to continue to work with data when they have no connection to the backend. And once the connection is restored, data will be uploaded and downloaded, and data conflicts will be flagged and resolved.

How does the Offline Sync feature work?

Figure-7-Offline-Sync

You can use Azure Mobile Apps Offline Sync when you use:

  • The Azure Mobile Apps as a backend for your mobile application
  • The Azure Mobile Apps SDK in your mobile application
  • A local datastore on the mobile device
    • by default, SQLite is used. On iOS, Core Data is used
    • you can also implement your own datastore

Here is a code example of how it works for a Windows-based mobile application. Don’t worry if you don’t understand all of the code. I’m only showing you this to convey how easy it is to get started with the Offline Sync feature.

This code is code on the client side – code in the mobile application. You can use this because the client uses the Azure Mobile SDK and has a client side datastore (SQLite) installed.

First, the code initializes a MobileServiceClient and gest a reference to a MobileServiceSyncTable. The table is based on the TodoItem class, which is a simple class that represents a todo item in the table.

public static MobileServiceClient MobileService = 
new MobileServiceClient("https://mymobileapp.azurewebsites.net");
      
private IMobileServiceSyncTable<TodoItem> todoTable = App.MobileService.GetSyncTable<TodoItem>();
Next, we initialize the local data store. In this case, the SQLite store.
private async Task InitLocalStoreAsync()
{
   if (!App.MobileService.SyncContext.IsInitialized)
      {
          var store = new MobileServiceSQLiteStore("localstore.db");
          store.DefineTable<TodoItem>();
          await App.MobileService.SyncContext.InitializeAsync(store);
      }

      await SyncAsync();
}

And finally, the code synchronizes the todoTable object with the server by calling the PullAsync method.

private async Task SyncAsync()
{
   await App.MobileService.SyncContext.PushAsync();
   await todoTable.PullAsync("todoItems", todoTable.CreateQuery());
}

This code enables the application to work when it is offline. The magic that you don’t see is what the Mobile Apps backend does for you. The combination of this code, the local datastore and the Mobile Apps backend is what make Offline Sync work.

Push notifications

Another common challenge that mobile developers face is sending push notifications to devices. A push notification is a small message that can pop up on the screen of your mobile device to alert you that something happened in the mobile application. You know what I’m talking about, you probably get way too many push notifications every day.

As a developer, you can send push notifications to a device by instructing a Platform Notification Service (PNS) to send one to a device. Every mobile platform has its own PNS. Apple has one, Android has one and Windows has another one. And these different PNSs expect different instructions and different ways of authentication and message formats. This makes it difficult to send a message to all the devices that use your application. It requires that you implement and maintain specific code to communicate with all of the different PNSs that you use.

Azure Mobile Apps Push notifications works with the Azure Notification Hubs service. This service is specifically made for the purpose of sending notifications and is coupled to Azure Mobile Apps to provide an easy experience. Here is how it works:

Figure-8-Push-Notifications

  1. The mobile application, that runs on the device, registers the device with the Platform Notification Services and gets handles back from these services
  2. The mobile application sends the handles from the PNSs to the application backend, which is the Azure Mobile App
  3. The Azure Mobile App registers the handle with the Azure Notification Hub for later use

Figure-9-Push-Notifications---2

Now, the app running in the Azure Mobile App, tells the Azure Notification Hub to send notifications and it will send those to all of the PNSs. It abstracts the PNSs so that you don’t have to implement specific code to talk to them. It also adds functionality such as the ability to group certain devices and filter devices when you send messages. This allows you to send more targeted notifications.

You can easily use a Notification Hub when you use the Push Notification feature in Azure Mobile Apps. This couples a Notification Hub to your Azure Mobile App and allows you to connect it to various PNSs.

What do you need to do to implement Push Notifications?

  1. Add a reference to the Notification Hubs assemblies to your Mobile App backend. You can do this by using a NuGet package
  2. Register the app for push notifications with the Platform Notification Services that you want to use
    a. You do this at the website of the PNS, for instance at the Apple PNS. This provides you with keys and sometimes certificates that you need to fill out in the Azure poral experience of Azure Mobile Apps
  3. Configure backend to send notifications
    a. Per PNS, you enter the security details that you’ve received in step 2
  4. Update your code
    a. Client:
    i. Register device with the PNSs and subscribe to notifications
    b. Backend
    i. Connect to Notification Hub
    ii. Send notifications

Summary

In this post, you’ve learned how Mobile Apps can serve as a backend for your mobile applications.

  • Azure Mobile Apps host APIs that you use from your mobile application
  • You can create the APIs for Mobile Apps in Node.js and .NET
  • Mobile Apps offers the Offline Sync feature. This enables users to continue to work when they are not connected to the backend
  • Offline Sync requires the Mobile Apps SDK and a data store (like SQLite) on the client
  • Push Notifications is a feature that allows Mobile Apps and other App Service types to send notifications to devices using the Azure Notification Hubs service