I originally wrote this in a comment, adding it here for visibility, hopefully it helps more people.
I got it to work on Android, here is what i'm using and what i did
- Visual Studio 2022 17.3.2 (NOT PREVIEW), (NOTICE: 17.3.3 just got released few hours ago, and i'm not upgrading yet).
- In your project, install the following Nuget Packages:
- Xamarin.Firebase.Messaging v123.0.7 (Latest at the time of writing)
- Plugin.FirebasePushNotification v3.4.25 (Latest at the time of writing)
- RESTART VISUAL STUDIO
- Add your google-service.json to Project Folder/Platforms/Android
- Right click the file, click properties.
- In Build Actions, select GoogleServiceJson
NOTICE: If you don't see Google ServicesJson, Restart Visual Studio.
In App.xaml.cs add the following in the constructor.
CrossFirebasePushNotification.Current.OnTokenRefresh += (s, p) =>
{
// PLACE BREAK POINT HERE TO CAPTURE TOKEN IN DEBUG
System.Diagnostics.Debug.WriteLine($"TOKEN : {p.Token}");
};
// Push message received event
CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
{
System.Diagnostics.Debug.WriteLine("Received");
};
//Push message received event
CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
{
System.Diagnostics.Debug.WriteLine("Opened");
foreach (var data in p.Data)
{
System.Diagnostics.Debug.WriteLine($"{data.Key} : {data.Value}");
}
};
In ProjectRoot/Platforms/Android/MainActivity.cs add the following
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
FirebasePushNotificationManager.ProcessIntent(this, Intent);
}
In ProjectRoot/Platforms/Android/MainApplication.cs add the following
public override void OnCreate()
{
base.OnCreate();
//Set the default notification channel for your app when running Android Oreo
if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
//Change for your default notification channel id here
FirebasePushNotificationManager.DefaultNotificationChannelId = "FirebasePushNotificationChannel";
//Change for your default notification channel name here
FirebasePushNotificationManager.DefaultNotificationChannelName = "General";
}
//If debug you should reset the token each time.
#if DEBUG
FirebasePushNotificationManager.Initialize(this, true);
#else
FirebasePushNotificationManager.Initialize(this,false);
#endif
//Handle notification when app is closed here
CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
{
};
}
Place a break point inside the event OnTokenRefresh in App.xaml.cs, run. Get Token. Use that token to test using Firebase Console