For a few recent projects, like my haveibeenpwned app I had to implement push notifications. Also this is one of the most frequently asked questions on the Xamarin forums which is not that odd because it is a main feature of almost every app.
Throughout a series of posts I will show you how you can implement push notifications in your Xamarin (Forms) app. This first of five(!!) posts will introduce you to the wonderful world of push. This introduction, the iOS app, Android app, Windows Phone app and the server-side of things will be handled.
Push notifications are a handy way of updating your users without them having to open up your app all the time or having you app poll a server, which has a negative impact on the battery-life as well.
But what would one need to implement push notifications?
- A server which sends out the notifications at certain triggers
- The app has to implement registering for push notifications
That’s right, just a simple client/server scenario. The server can be anything: a console application which runs on a timed schedule, the result of a call to your REST API back-end or a trigger when certain data is inserted on your database.
Unfortunately it’s not that easy.
If you look at the bigger picture there is another party involved which would be the provider of the notifications;
- Apple Push Notification service (APNs)
- Google Cloud Messaging (GCM)
- Windows Push Notification Service (WNS)
All your push notifications have to go through their corresponding providers service.
This adds a first layer of complexity on both you server and client side. You have to send the same push notification to three providers from your server and also have to implement three ways of registering for push on the client side.
Luckily for us lazy developers other developers have solved this for us. There are a number of (free) services which provide a solution for having to send custom notifications to Apple, Google and/or Microsoft. One of the most well-known is Parse by Facebook. But beware; they are pulling the plug!
Why, I hear you asking? Because Azure is awesome. They offer great services at low prices or even free in some cases! Notification Hub being one of them. As long as you don’t have over 500 device registrations and don’t send over a million notifications you can do it for nothing! And beyond that the pricing is reasonable as well.
Also it is not very likely they will kill this service at least not while providing a reasonable alternative. And you are using Xamarin! So you want to use .net and chances are your backend will me .net as well so while you’re at it host that in Azure to and manage it all from one place. Your push server can easily be setup with a REST server in Azure Web Apps or that console application I mentioned earlier with WebJobs. Again they have a free variant to get you a head start.
Lastly everyone seems to use Azure nowadays. That shouldn’t be a reason for you to follow blindly, but it does mean that there is a lot of documentation and even SDKs you can leverage.
The thing all these services have in common is that you have one entry point to send messages across all platforms, they provide a layer of abstraction for you so you can treat push notifications as a whole instead of three separate providers.
In the case of Azure you even have the possibility to create universal push messages. More about this later.
Also they take over the registration administration of devices for you and add a layer of abstraction where you can work with so-called tags. You can let a device subscribe on one or more tags which can be anything from a unique identifier to a news category on which hundreds, thousands or even millions of devices can be registered. Then when there is breaking news in a certain category just send out your message on the corresponding tag and Azure will determine which devices have to receive it. Again; more on this later on.
On the client side we’re going to have to implement some platform specific code. There is no escaping that. And although there is at least one good plugin available by rdelrosario I will be implementing the code on the apps manually to give you a better understanding of what is going on.
So that takes care of the basic understanding of push notifications. The next posts will be about implementing the app code and lastly the server code bringing it all together. If you have any request about what you would like to read or questions, just let me know!
Second part: Push notifications with Xamarin – iOS