Press "Enter" to skip to content

Push notifications made easy in Xamarin

I have been working with integrating push notifications in mobile apps for a while now and got a bit frustrated about going to the same process each time on every project. So I decided to create a few plugins that makes life easier when working with push notifications:

  • Azure Push Notification: Receive and handle azure push notifications. This plugin is used when working with Azure Notification Hubs.

  • Firebase Push Notification: Receive and handle firebase push notifications. This plugin is used when working with Firebase Cloud Messaging on iOS and Android.

  • Push Notification: Receive and handle push notifications with FCM on Android and APS on iOS. This is kind of the plain version of the two above, if you are the one handling and managing the push notifications on your backend yourself or maybe using other third party to handle this that isn't Azure or Firebase then this is the ideal plugin for you.

General Features

  • Receive push notifications: Trigger events when notification is received, opened and token is refreshed.
  • Localization: Supports multilingual notifications based on notification keys.
  • Notification 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".

Notification Actions

Specific Features

Azure Push Notification
Azure notification hubs have this awesome feature called tags, that let us group devices registered within a same tag.

Register tags

   /// <summary>
   /// Registers tags in Azure Notification hub
   /// </summary>
    await CrossAzurePushNotification.Current.RegisterAsync(new string[]{"crossgeeks","general"});

Note: The method above cleans all previous registered tags when called. So it kind of replaces the tags each time you call it.

Unregister tags

   /// <summary>
   /// Unregister all tags in Azure Notification hub
   /// </summary>
    await CrossAzurePushNotification.Current.UnregisterAsync();

Firebase Push Notification

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.

//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();

Events

All these plugins have the following events:

OnTokenRefresh - Once token is registered/refreshed you will get it on this event.

OnNotificationReceived - When a push notification is received you will get the notification payload data in this event. On iOS by default you need to tap the notification to receive the data.

OnNotificationOpened - When a push notification is opened you will receive the payload data in this event. This is ideal to navigate to other place in your app.

OnNotificationDeleted - This event only gets triggered on Android when you dismiss a notification. You will get the payload data of the dismissed notification here as well.

OnNotificationError - If any errors occurs you will get a string with the error message on this event.

You can use these plugins on traditional Xamarin or Xamarin Forms projects.

Worth noting that there are other really good options to handle easily push notifications:

Need your help to make these plugins even greater. There's still a lot todo.

Roadmap

  • UWP Support
  • MacOS Support
  • Tizen Support
  • Badges Support
  • In line reply notifications
  • Easier customization
  • Picture Messages
  • Inbox Stacking
  • Better error handling
  • Samples with diferent scenarios

For more information on documentation, getting started, features or collaboration here are the repositories:

Happy push notifications!

2 Comments

  1. Hi,
    Great work! I use Azure Notification Hub connected to FCM for Android and APNS for iOS. In
    Xamarin.Forms code I have the traditional registration code found here:https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter5/concepts/

    So my question is which plugin would suit my setup and can I then remove that whole plattform specific registration/notification code in my codebase? Where are the events handled (PCL, plattform specific projects)

    Sample would help.

    best
    Eric

Leave a Reply

Your email address will not be published. Required fields are marked *