Press "Enter" to skip to content

Firebase push notifications made simple in Xamarin

Working with push notifications can be challenging because each platform has its own way of implementing it. Firebase Cloud Messaging allows you to send/receive notifications to a specific device or topics based. It has components for Xamarin iOS and Xamarin Android but setting them up and getting it to work properly might be frustrating especially when you are new into this subject. That’s why just released a plugin to make this simpler to setup and handle. It’s basically an abstraction of the common parts but also providing enough flexibility to customize. In this article will show the plugin features and a step by step video of setting it up in your project.

Plugin Features

  • Receive firebase push notifications
  • Subscribing/Unsubscribing to topics
  • Support for push notification category actions
  • Customize push notification

Events

The following events will allow you to have push notification related feedback across iOS and Android:

OnTokenRefresh – Provides feedback when token is updated.

CrossFirebasePushNotification.Current.OnTokenRefresh += (s,p) =>
{
    System.Diagnostics.Debug.WriteLine($"TOKEN : {p.Token}");
};

OnNotificationReceived – Provides feedback when notification is received.

CrossFirebasePushNotification.Current.OnNotificationReceived += (s,p) =>
  {
      System.Diagnostics.Debug.WriteLine("Received");
  };

OnNotificationOpened– Provides feedback when notification is opened.

 CrossFirebasePushNotification.Current.OnNotificationOpened += (s,p) =>
  {
                System.Diagnostics.Debug.WriteLine("Opened");
                foreach(var data in p.Data)
                {
                    System.Diagnostics.Debug.WriteLine($"{data.Key} : {data.Value}");
                }

                if(!string.IsNullOrEmpty(p.Identifier))
                {
                    System.Diagnostics.Debug.WriteLine($"ActionId: {p.Identifier}");
                }

 };

Cross platform topic subscription

Topic subscription allows you to group devices within a particular topic. In a way that only devices in subscribing to a specific topic will get push notifications for that topic. You can subscribe to topics on iOS and Android in the same way.

//Subscribing to single topic
CrossFirebasePushNotification.Current.Subscribe("general");

//Subscribing to multiple topics
CrossFirebasePushNotification.Current.Subscribe(new string[]{"baseball","football"});

//Unsubscribing to single topic
CrossFirebasePushNotification.Current.Unsubscribe("general");

//Unsubscribing from multiple topics
CrossFirebasePushNotification.Current.Unsubscribe(new string[]{"food","music"});

//Unsubscribing from all topics
CrossFirebasePushNotification.Current.UnsubscribeAll();

Notification Category Actions

It’s very handy nowadays to have push notifications with buttons, that allows our applications to get an instant response from the user by just tapping on a notification button. For example, if your application sends friend requests to users might be useful to have two options “Accept” and “Reject”.


Push Notification Handler

On Android, you are able to override the default behavior when a notification arrives by implementing IPushNotificationHandler and initializing the plugin with your own implementation that will allow you to provide any customization based on your needs. More details on this on the full documentation referenced on the end of this article.

To get started you must get everything ready on firebase portal. You can follow this steps: Firebase Setup

Here a brief step by step video on setting it up in your project:

Check out the full documentation here: Firebase Push Notification Plugin

Happy Pushing Notifications!

7 Comments

  1. Sid Sid

    Hi,

    that’s really usefull but I’m facing some issue with the last release 2.0.1-alpha
    I configured everthing as explained in your tutorial but I didn’t receive anything on iPhone (iPhone 5 / 6 / 7).

    Apps is asking me for authorization but I never receive anythings and I tryed with all the payloads published in your github tutorial.

    What can I do to fix it?

    Regards

    • Rendy Rendy

      Did you get to solve your issue?

  2. Adigma Adigma

    Unfortunately unsubsribing from Topics is not working at all.
    Messages still come through.
    Anybody else noticed this issue ?

  3. Rendy Rendy

    Do you get any errors on the console?

    Is this on iOS or Android?

    Where are you unsubscribing?

  4. Mohammed Mohammed

    hello
    it’s a great plugin thanks
    but I have an issue I did everything as you explain on android an got notifications and everything is fine but on ios, I got nothing no token no notifications when I’m on firebase console it shows me there is a users on ios but nothing work

  5. venkanna venkanna

    Hi
    Am Following Same Manner But its Getting a problem in LoadApplication(new App());
    Object Reference Not set to an instance of an object .

    Am using Xmarian.forms i need to implement fcm both android and IOS.

  6. Pınar Can Pınar Can

    Hi, it is really helpfull but i’m confused some point; where can we send the notification in project? From code-behind or from where?

Comments are closed.