Quantcast
Viewing all articles
Browse latest Browse all 6

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.

Image may be NSFW.
Clik here to view.

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”.

Image may be NSFW.
Clik here to view.

Image may be NSFW.
Clik here to view.

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!

The post Firebase push notifications made simple in Xamarin appeared first on Xamboy.


Viewing all articles
Browse latest Browse all 6

Trending Articles