Mobile Push Notifications with Azure
Two ways to integrate a mobile application with Azure and send push notifications will be explored here.
Both ways will be tried here using a sample app written with Xamarin Forms, a framework that aims to take mobile cross-platform in .NET.
The code of the app may be found here:
Communicating with FCM (Firebase Cloud Messaging)
google.services.json
The file commonly known as “google.services.json” is used by the Google Services Gradle Plugin at build time to configure the integration with Google API and Firebase.
Getting one will require to create a Firebase project as documented in the following section.
Firebase server key
This is another piece of information that is needed to use FCM and will be needed with both methods.
The first way, Azure Notification Hub
The process to follow is well detailed in this Microsoft tutorial:
I have pictured in this image the flow of a notification:
And here is the result:
These are the lines of code I needed to add in order to have the project up and running:
The second way, Visual Studio App Center
Visual Studio App Center is the new kid on the block.
It offers a different approach as it tries to give more and asking for less.
This stack is also cloud-agnostic, exposing an Open API interface easy to integrate.
The steps required to add the SDK to your Xamarin App are well outlined in the following document:
As I am writing this article, however, I had to find a workaround related to some conflict with nuget packages as fixed here:
However, there is some problem on the SDK side and the push notification reaches the emulator but never gets parsed:
10-06 22:50:43.774 D/AppCenterPush( 7240): Push standard data: {collapse_key=it.albertodenatale.pushnotifications, google.original_priority=high, google.sent_time=1570395044141, google.delivered_priority=high, gcm.notification.e=1, gcm.notification.title=141414, google.c.a.e=1, google.ttl=2419200, from=226661047026, google.message_id=0:1570395044148064%06ca70f606ca70f6, gcm.notification.body=141414}10-06 22:50:43.774 D/AppCenterPush( 7240): Push custom data: {}10-06 22:50:43.796 W/FirebaseMessaging( 7240): Error while parsing timestamp in GCM event10-06 22:50:43.796 W/FirebaseMessaging( 7240): java.lang.NumberFormatException: s == null10-06 22:50:43.796 W/FirebaseMessaging( 7240): at java.lang.Integer.parseInt(Integer.java:577)10-06 22:50:43.796 W/FirebaseMessaging( 7240): at java.lang.Integer.valueOf(Integer.java:801)10-06 22:50:43.796 W/FirebaseMessaging( 7240): at com.google.firebase.messaging.zzd.zzb(Unknown Source:60)10-06 22:50:43.796 W/FirebaseMessaging( 7240): at com.google.firebase.messaging.zzd.zzg(Unknown Source:26)10-06 22:50:43.796 W/FirebaseMessaging( 7240): at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(Unknown Source:160)10-06 22:50:43.796 W/FirebaseMessaging( 7240): at com.google.firebase.iid.zzg.run(Unknown Source:26)10-06 22:50:43.796 W/FirebaseMessaging( 7240): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)10-06 22:50:43.796 W/FirebaseMessaging( 7240): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)10-06 22:50:43.796 W/FirebaseMessaging( 7240): at java.lang.Thread.run(Thread.java:764)10-06 22:50:43.796 W/FirebaseMessaging( 7240): Unable to log event: analytics library is missing10-06 22:50:43.798 W/FirebaseMessaging( 7240): Error while parsing timestamp in GCM event10-06 22:50:43.798 W/FirebaseMessaging( 7240): java.lang.NumberFormatException: s == null10-06 22:50:43.798 W/FirebaseMessaging( 7240): at java.lang.Integer.parseInt(Integer.java:577)10-06 22:50:43.798 W/FirebaseMessaging( 7240): at java.lang.Integer.valueOf(Integer.java:801)10-06 22:50:43.798 W/FirebaseMessaging( 7240): at com.google.firebase.messaging.zzd.zzb(Unknown Source:60)10-06 22:50:43.798 W/FirebaseMessaging( 7240): at com.google.firebase.messaging.zzd.zzj(Unknown Source:2)10-06 22:50:43.798 W/FirebaseMessaging( 7240): at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(Unknown Source:201)10-06 22:50:43.798 W/FirebaseMessaging( 7240): at com.google.firebase.iid.zzg.run(Unknown Source:26)10-06 22:50:43.798 W/FirebaseMessaging( 7240): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)10-06 22:50:43.798 W/FirebaseMessaging( 7240): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)10-06 22:50:43.798 W/FirebaseMessaging( 7240): at java.lang.Thread.run(Thread.java:764)
Push Notifications using App Center, in general, come with an SDK client-side and are easier to configure offering a higher level of abstraction.
After some work troubleshooting and some help, the notification happily showed:
The code written to integrate with App Center may be read in this commit:
Conclusions
In this walkthrough, it has been tried two ways of providing push-notifications to apps. The first more mature is by using Notification Hub that offers some libraries to be installed mobile side. The second is by using the very recent Visual Studio App Center that offers a higher level of abstraction.