Skip to content

Instantly share code, notes, and snippets.

@jfversluis
Forked from svedmark/FCMService.cs
Created May 23, 2022 13:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfversluis/dd760594099066c7450d50cce6499368 to your computer and use it in GitHub Desktop.
Save jfversluis/dd760594099066c7450d50cce6499368 to your computer and use it in GitHub Desktop.
Minimal FCM Push Messaging for .NET MAUI (RC3)
* Setup Firebase project like normal and add google-services.json (make sure Build action is set to GoogleServicesJson)
* Nugets required for MAUI project file (later versions don't work)
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0-android'">
<PackageReference Include="Xamarin.Firebase.Iid" Version="121.1.0" />
<PackageReference Include="Xamarin.Firebase.Messaging" Version="122.0.0" />
<PackageReference Include="Xamarin.Google.Dagger" Version="2.39.1" />
<PackageReference Include="Xamarin.GooglePlayServices.Base" Version="117.6.0.5" />
</ItemGroup>
using Android.App;
using Android.Content;
using AndroidX.Core.App;
using Firebase.Messaging;
namespace FCMApp.Android;
[Service(Exported = false)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FCMService : FirebaseMessagingService
{
public override void OnMessageReceived(RemoteMessage message)
{
RemoteMessage.Notification notification = message.GetNotification();
IDictionary<string, string> data = message.Data;
string body = "";
string title = "";
if (data != null && data.ContainsKey("body") && data.ContainsKey("title"))
{
body = data["body"];
title = data["title"];
}
else if (notification != null)
{
body = message.GetNotification().Body;
title = message.GetNotification().Title;
}
if (!string.IsNullOrEmpty(body) && !string.IsNullOrEmpty(title))
{
// TODO: Manage this message (we get here only when app is in foreground)
}
}
}
namespace FCMApp;
public interface IMessagingService
{
string GetToken();
bool IsMessagingAvailable();
void SetNewTokenCallback(Action<string> token_callback);
}
using Android.Gms.Common;
using Android.Gms.Tasks;
using Firebase.Iid;
namespace FCMApp;
public partial class MessagingService
{
public partial bool IsMessagingAvailable() => GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(Android.App.Application.Context) == ConnectionResult.Success;
public partial string GetToken() => FirebaseInstanceId.Instance.Token;
public partial void SetNewTokenCallback(Action<string> token_callback)
{
if (token_callback == null)
throw new InvalidOperationException("Callback cannot be null");
if (GetToken() != null)
token_callback(GetToken());
else
FirebaseInstanceId.Instance.GetInstanceId().AddOnSuccessListener(new OnTokenListener((token) => token_callback(token)));
}
}
public class OnTokenListener : Java.Lang.Object, IOnSuccessListener
{
Action<string> _callback;
public OnTokenListener(Action<string> success_callback) => _callback = success_callback;
public void OnSuccess(Java.Lang.Object result) => _callback?.Invoke(FirebaseInstanceId.Instance.Token);
}
namespace FCMApp;
public partial class MessagingService : IMessagingService
{
public partial string GetToken();
public partial bool IsMessagingAvailable();
public partial void SetNewTokenCallback(Action<string> token_callback);
}
@peter-bozovic
Copy link

This was very helpful ! Thanks :)

Do you have something similar on how to use FCM on iOS with MAUI ? Or which library works with current MAUI version for pushing notifications on iOS ?

@jfversluis
Copy link
Author

Thanks @peter-bozovic ! Did you get it to work with this?

I don't have anything for iOS right now yet

@peter-bozovic
Copy link

This code gave me some first results and put me on the right track. (mostly the working packages and OnMessageReceived service)
However it uses a lot of obsolete methods so I had to rewrite almost everything from parts found in different projects / documentations / samples ...

@AnjanaJois20
Copy link

Do we have any way to implement push notification sin MAUI

@jfversluis
Copy link
Author

@AnjanaJois20 this code is for .NET MAUI or Xamarin.Forms for that matter. There is no difference between it

@maurobernal
Copy link

FirebaseInstanceId is deprecated.

@michalpr
Copy link

michalpr commented Oct 25, 2022

I just put together a tutorial how to get work FCM and .NET MAUI for iOS platform. Hopefully will help someone...

@jfversluis
Copy link
Author

Awesome @michalpr! Thanks for that!

@jsanjuan2016
Copy link

@maurobernal
Replace the deprecated FirebaseInstanceId with FirebaseMessaging.


- FirebaseInstanceId.Instance.Token -> (string)FirebaseMessaging.Instance.GetToken().Result
- FirebaseInstanceId.Instance.GetInstanceId().AddOnSuccessListener(... -> FirebaseMessaging.Instance.GetToken().AddOnSuccessListener(...
- .Invoke(FirebaseInstanceId.Instance.Token) -> .Invoke((string)FirebaseMessaging.Instance.GetToken()

@alexanderokosten
Copy link

I have a clarifying question. How can I send a push inside my application to a user via FCM using the Messaging Service or FCMService? Please give examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment