Skip to content

Instantly share code, notes, and snippets.

@jfversluis
Forked from svedmark/FCMService.cs
Created May 23, 2022 13:40
Show Gist options
  • 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);
}
@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

@vovanb
Copy link

vovanb commented May 27, 2024

do you have working MAUI example for android?

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