Search code examples
androidxamarinpush-notificationgmail

How to receive Gmail notifications with Xamarin


I know this seems like a basic question, but I can't find a clear answer to it. I want to develop an Android app with Xamarin to receive push notifications (new messages) from a Gmail account. I know the native Gmail app already does this, but for a few reasons I need to implement this funcionality myself. I searched about GCM, FCM, but I haven't found a single guide or example on how to do this. Please help!


Solution

  • I think the challenging bit here is the Gmail part.

    For your scenario to work:

    • Gmail will need to forward its changes to a server-side application that you control
    • This server-side application will need to create a push notification by sending it to GCM/FCM's web services
    • From here you will need to implement push notifications normally in a Xamarin.Android app

    I don't think you are going to find a prebuilt/off-shelf solution that achieves this.

    This is a pretty tall order to implement yourself, but it seems to be quite possible. I will walk through what I would do to make this work:

    • Implement an Azure Function to act as a webhook for Gmail. Docs for Gmail here. I don't know if there is an example for this part.
    • Use Azure Notification Hubs to send push notifications. Your webhook will forward notifications to the hub. I have a sample here of sending from an Azure function.
    • Integrate the Azure Notification Hub SDK into your Xamarin.Android app

    Like I said, this is quite a bit of work. I'm not sure if you have Windows Azure or Xamarin experience, but I think this is the easiest path forward for a new project using C#.

    I have a book here (make sure you get 3rd edition) that covers push notifications and Azure functions in Xamarin apps. The sample code for the book is on Github if you want a free resource.