• Setup Xamarin Forms Android App for Push Notifications

    Now that we have setup Microsoft Azure, Google Firebase Cloud Messaging and also our new Azure Mobile App, it’s time to update our existing Xamarin Forms App. First we start with the Android part as this is the easy one because it works on both Windows and Mac and push notifications also work fine in the Android Emulator (in opposite to Apples iPhone Simulator because for testing push notifications on iPhone you need iPhone hardware).

    This posting is part of a series of 3 postings regarding push notifications with Xamarin Forms:

    1. Configure Azure and Google Firebase for push notifications in Xamarin Forms
    2. Setup server part for Xamarin Forms Push Notifications
    3. Setup Xamarin Forms Android App for Push Notifications (the current posting)

    Setup Android part in Xamarin Forms App

    A good explanation from Microsoft could be found in their Azure docs, but it’s tailored for their ToDoItemManager Demo so we have to do some more work to implement it to our existing app from scratch. Here are the steps to be done. And don’t worry if you get errors while you are adding all the code. They will disappear as soon as you have completed all the steps.

    1. In your PCL project, add a new class “PushClient.cs”. (Microsofts demo has it in the TodoItemManager.cs but as we want to add the push notification to our own app we have to create a new class in the PCL for this code). Remember your namespace and replace the whole file with the following code:
    2. Afterwards do the following changes in your PushClient.cs:
      1. Replace your namespace in line 4.
      2. In line 10, set your correct application url from Azure. If you forgot: You will find it in your Azure Portal at “App Services” -> Your Push Notification App -> “Overview” at “URL” at the right. Take care about the protocol: I have no idea why Azure shows “http://YourURL.azurewebsites.net” because Microsoft has a wildcard SSL certificate for *.azurewebsites,net for everyone for free (see here). So I would always advise to use https instead of http for every transfer so check whether your URL works fine with https and then use this protocol.
    3. Now go to your Droid project, right-click on “Components” and select “Get more components”. In section “Cloud Services” you’ll find “Google Cloud Messaging Client“. Don’t be afraid about the name (GCM, not FCM) nor the old version (current release 1.0 is from December 2013), it still works fine also with Firebase. So add it to your Droid Project.
    4. Open MainActivity.cs and add “using Gcm.Client;” to the top.
    5. Within “OnCreate” method, add this piece of code right after the line “LoadApplication”:
    6. In above code, a new method “CreateAndShowDialog” has been used so we need to add it to the MainActivity.cs. Additionally we have to create a new MainActivity instance for execution in main UI thread. Personally I like to structure the code a bit so it’s easier months or years later to find out why some code has been added. So here is the code to be added to the MainActivity.cs encapsulated in a region.
    7. Finally we also have to set the new MainActivity instance to current one. Add it to the beginning of “OnCreate” in MainActivity.cs.
    8. Next we need to create a new class. So right-click on your Droid Project, select “Add” -> “New File”, select “General” -> “Empty Class” and choose name “GcmService.cs”.
    9. Now we need to add several code blocks. In the end, we replace the empty class completely. So just note down your namespace (“MyProject.Droid”), clear all code from new GcmService.cs and insert the following code. Afterwards, replace “MyProject.Droid” with your own namespace!

    10. Some modifications have been done to the above listed code:
      1. As said, replace namespace “MyProject.Droid” in line 22 with your own namespace.
      2. In line 30, replace “<PROJECT_NUMBER>” with the Project Number from your Google Firebase Cloud Messaging Project. You will find it on “Project Settings” -> “Cloud Computing”, it’s named “Sender ID”. It’s a number with 13 digits.
    11. Now clean your solution and rebuild it. It should compile fine.

Leave a comment

If you want to share your opinion, leave a comment.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">