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!

11 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

  2. bill bill

    Looks great. Wish I’d found this earlier. I am building the individual pieces for an app and finished (mostly) the UWP and iOS notifications portions and I’m now starting the Android/FCM notification part. All notifications are originating with Azure Notification Hubs.

    I wanted to implement action buttons in the notification from the notification tray, just like you see with Windows 10 email. I got it working for UWP and iOS, where the notification service puts the notification in the tray with action buttons on it. In those scenarios, I don’t have to intercept the message and add the buttons myself, it’s just handled (and with localization, no less). In those cases the app is backgrounded and never even launches unless you click on an action. .

    But I can’t find any way to do this with Android. Would you know if it’s even possible? Or, if want to put action buttons on a notification, do we really have to intercept the message, create our own message and put it in the tray manually?

    Usually it seems like there’s feature parity between these platforms, but I can’t find it in this case.

    TIA

    • Rendy Rendy

      Hi bill

      Is great that you have that working on UWP, this is want of the things I will like to add to the plugin UWP support with notification actions, if you could do a PR with this will be awesome or point me into some reference to achieve this. On other topic the plugin does supports displaying notification actions on Android and iOS.

  3. Hi Rendy,
    great plugin! I am using Azure Notification Hubs and got up and running fairly easily with a Xamarin Forms app. However, it seems it is not possible to send template notifications? At least for me, notifications using hub.SendTemplateNotificationAsync do not arrive, while e.g. using hub.SendAppleNativeNotificationAsync works fine. Are templates supported by the plugin? Even if it is not directly supported, perhaps I could inject template registration from the app, and hope that the OnNotificationReceived event would be triggered as usual?

    I am also wondering if you have any thoughts on badge support.

    Thanks and best regards,
    Thorvald

  4. Mario Kielblock Mario Kielblock

    Hi Rendy,

    I have the same question as Thorvald, is there anyway to register with a template, so that when sending Template notifications from the azure hub it would be delivered to all device types?

    Kind Regards
    Mario

    • Mario Kielblock Mario Kielblock

      Few months down the line tried the plugin again and forgot the issue with templates, guess I’ll be sending them separately

  5. NIRAJKUMAR BHATT NIRAJKUMAR BHATT

    Hi,
    Thank you for providing these amazing nuget packages. I am using PushNotification.
    I am using AWS SNS to send push notification and use PushNotification Plugin. This requires me to create an endpoint in AWS SNS based on registration id provided by FCM. I can create AWS SNS endpoint sucessfully. However when I am trying to publish a message, AWS SNS is not able to send it to FCM.
    I got following log details from AWS:
    {
    “notification”: {
    “messageMD5Sum”: “865b4a963a8fb63decf95e0d2e669ac8”,
    “messageId”: “51ad9f82-9d8b-5359-a926-cc491e3db3d2”,
    “timestamp”: “2018-07-20 06:30:14.294”
    },
    “delivery”: {
    “deliveryId”: “bf71f298-75e8-5026-9451-a153f65f6687”,
    “destination”: “arn:aws:sns:ap-southeast-2:071359012260:endpoint/GCM/NirajBhaNotificationAp/d581e879-ff43-33a7-8dda-8109d24787e1”,
    “providerResponse”: “{\”multicast_id\”:8954496440032001200,\”success\”:0,\”failure\”:1,\”canonical_ids\”:0,\”results\”:[{\”error\”:\”NotRegistered\”}]}”,
    “dwellTimeMs”: 27,
    “attempts”: 1,
    “token”: “fvZwuD7W8tQ:APA91bEj4fq0CRYrUPp24WzKvVWjwpc4kVdifUKy_rwgHHLZTayeZzJAsPlKiAY3Lv58Lw3ttbmEGY-NlYCo0nJkAzyQm2owBI-P0-lMb0BQcUFwnGQUwyYONzSubbtKSHjE_k0P4_z-XNNpVf75g07j25CBCnwcWw”,
    “statusCode”: 200
    },
    “status”: “FAILURE”
    }

    I know that if APP is in debug, it will not receive push notification. So I closed and reopen APP and then closed again. Still the same problem.
    So I think FCM is dropping registration id for my device or it is issuing a new registration id but I am not able to receive it in PushPlugin. I have subscribed to OnTokenRefresh event but it doesn’t get hit meaning it is not receiving new token from FCM.

    Please let me know if you have any pointer.

    • NIRAJKUMAR BHATT NIRAJKUMAR BHATT

      It seems like Firebase is invalidating the token soon after issuing it.
      If I pick up value of CrossPushNotification.Current.Token and try to send a push notification through Firebase, it throws an error saying unregistered registration token.

  6. Jack Sojourn Jack Sojourn

    Wondering if this can be used for silent notifications?

  7. I have faced push notification problems, but I found my solution here. I am not using Azure and Firebase, but I will download other Push Notification plugin which you have mentioned for my problems in the notification. Thanks for the solution.

Comments are closed.