Firebase setup, OnMessageReceived, device tokens.
Firebase Setup
- Create a Firebase project at console.firebase.google.com.
- Add an Android app — enter your package name exactly as in AndroidManifest.xml.
- Download
google-services.json and place it in Platforms/Android/.
- Install
Plugin.Firebase.CloudMessaging via NuGet.
// Platforms/Android/MyFirebaseMessagingService.cs
[Service(Exported = false)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFcmService : FirebaseMessagingService
{
public override void OnMessageReceived(RemoteMessage msg)
{
var n = msg.GetNotification();
ShowLocalNotification(n?.Title, n?.Body);
}
public override void OnNewToken(string token)
{
// Send token to your server so it can target this device
SendTokenToServer(token);
}
}
🔔The FCM token uniquely identifies the device+app pair. Send it to your backend whenever OnNewToken fires — it can change after app reinstalls.
Key Takeaways
google-services.json links your app to Firebase — place it in Platforms/Android/
OnNewToken fires when the FCM token is issued or refreshed
OnMessageReceived handles messages when the app is in the foreground
Create a notification channel on Android 8+ (API 26+) for background notifications