Reader small image

You're reading from  Xamarin 4.x Cross-Platform Application Development - Third Edition

Product typeBook
Published inDec 2016
Reading LevelIntermediate
Publisher
ISBN-139781786465412
Edition3rd Edition
Languages
Tools
Right arrow
Author (1)
Jonathan Peppers
Jonathan Peppers
author image
Jonathan Peppers

Jonathan Peppers is a Xamarin MVP and lead developer on popular apps and games at Hitcents such as the Hanx Writer (for Tom Hanks) and the Draw a Stickman franchise. Jon has been working with C# for over 10 years working on a wide range of projects at Hitcents. Jon began his career working Self-Checkout software written in WinForms and later migrated to WPF. Over his career, he has worked with many .NET-centric technologies such as ASP.Net WebForms, MVC, Windows Azure, WinRT/UWP, F#, and Unity3D. In recent years, Hitcents has been heavily investing in mobile development with Xamarin, and has development over 50 mobile applications across multiple platforms.
Read more about Jonathan Peppers

Right arrow

Chapter 9. Web Services with Push Notifications

Modern mobile applications are defined by their network connectivity. A mobile app that does not interact with a web server is both a rare find and potentially a boring application. In this book, we'll use the Windows Azure cloud platform to implement a server-side backend for our XamSnap application. We'll use a feature called Azure Functions, which is an excellent fit as a simple backend for our application and can send push notifications via Azure Notification Hubs. Once we are done with this chapter, our XamSnap sample application will be much closer to being a real application and will allow its users to interact with one another.

In this chapter, we will cover the following topics:

  • The services offered by Windows Azure

  • Setting up your Azure account

  • Azure Functions as a backend for XamSnap

  • Implementing a real web service for XamSnap

  • Writing client-side code for calling Azure Functions

  • Using the Apple Push Notification service

  • Sending notifications...

Learning Windows Azure


Windows Azure is an excellent cloud platform released by Microsoft in 2010. Azure provides both Infrastructure as a Service (IaaS) and Platform as a Service (PaaS) for building modern web applications and services. This means that it provides you the access directly to virtual machines within which you can deploy any operating system or software of your choice. This is known as IaaS. Azure also provides multiple platforms for building applications, such as Azure Web Apps or SQL Azure. These platforms are known as PaaS, since you deploy your software at a high level and do not have to deal directly with virtual machines or manage software upgrades.

Let's go over the following more common services provided by Windows Azure:

  • Virtual Machines: Azure provides you the access to virtual machines of all sizes. You can install practically any operating system of your choice; there are many premade distributions to choose from within Azure's gallery.

  • Web Apps: You can deploy...

Exploring Azure Functions


For the server side of XamSnap, we'll use Azure Functions along with Azure Storage Tables to provide backend storage to the application. Azure Functions is a simple solution to accelerate development for server-side applications that can leverage all features of Windows Azure. We will use the standard HttpClient class found in the .NET base class library for interacting with the service from C#.

A few neat features of Azure Functions are as follows:

  • You can write functions in a variety of programming languages such as JavaScript, C#, Python, and PHP, as well as some scripting languages such as Batch, Bash, and PowerShell

  • Azure Functions integrates with Visual Studio Team Services, Bitbucket, and GitHub for Continuous Integration (CI) scenarios

  • You can set up authentication easily with Azure Active Directory, Windows Live ID, Facebook, Google, and Twitter

  • Functions can be triggered via HTTP, a schedule or timer, Azure Queue, and so on

  • Azure Functions is truly serverless...

Using the Apple Push Notification service


Implementing push notifications with Azure Notification Hubs on iOS is very simple to set up from Azure's perspective. The most complicated part is working through Apple's process of creating certificates and provisioning profiles in order to configure your iOS application. Before continuing, make sure you have a valid iOS Developer Program account, as you will not be able to send push notifications without it. If you are unfamiliar with the concept of push notifications, take a look at Apple's documentation at http://tinyurl.com/XamarinAPNS.

To send push notifications, you need to set up the following:

  • An explicit App ID registered with Apple

  • A provisioning profile targeting that App ID

  • A certificate for your server to trigger the push notification

Apple provides both a development and production certificate, which you can use to send push notifications from your server.

Setting up your provision profile

Let's begin by navigating to http://developer.apple...

Implementing Google Cloud Messaging


Since we have already set up everything we need in the shared code and on Azure, setting up push notifications for Android will be a lot less work at this point of time. To continue, you will need a Google account with a verified e-mail address; however, I would recommend using an account registered with Google Play, if you have one. You can refer to the full documentation on Google Cloud Messaging (GCM) at https://developers.google.com/cloud-messaging/.

Tip

Note that Google Cloud Messaging requires that Google APIs be installed on the Android device and that the Android OS be at least Version 2.2.

Begin by navigating to http://cloud.google.com/console, then and perform the following steps:

  1. Click on the Create Project button.

  2. Enter an appropriate project name, such as XamSnap.

  3. Agree to the Terms of Service.

  4. Click on the Create button.

  5. When creating your first project, you may have to verify the mobile number associated with your account.

  6. Note the Project Number...

Summary


In this chapter, we went over what Windows Azure provides: Infrastructure as a Service and Platform as a Service. We set up a free Windows Azure account and set up an Azure Function App instance. We implemented the client-side code for making requests against our Azure Functions. Lastly, we implemented push notifications for iOS using Azure Notification Hubs to consolidate messages going to iOS devices via the Apple Push Notification service and Android via Google Cloud Messaging.

Using Azure Functions, we were able to get by without writing too much server-side code. In the next chapter, we'll explore how to use third-party libraries with Xamarin. This includes everything from the Xamarin Component Store to using native Objective-C or Java libraries.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Xamarin 4.x Cross-Platform Application Development - Third Edition
Published in: Dec 2016Publisher: ISBN-13: 9781786465412
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jonathan Peppers

Jonathan Peppers is a Xamarin MVP and lead developer on popular apps and games at Hitcents such as the Hanx Writer (for Tom Hanks) and the Draw a Stickman franchise. Jon has been working with C# for over 10 years working on a wide range of projects at Hitcents. Jon began his career working Self-Checkout software written in WinForms and later migrated to WPF. Over his career, he has worked with many .NET-centric technologies such as ASP.Net WebForms, MVC, Windows Azure, WinRT/UWP, F#, and Unity3D. In recent years, Hitcents has been heavily investing in mobile development with Xamarin, and has development over 50 mobile applications across multiple platforms.
Read more about Jonathan Peppers