📱 Device Features
Push Notifications with FCM
Firebase setup, OnMessageReceived, device tokens.

Firebase Setup

  1. Create a Firebase project at console.firebase.google.com.
  2. Add an Android app — enter your package name exactly as in AndroidManifest.xml.
  3. Download google-services.json and place it in Platforms/Android/.
  4. Install Plugin.Firebase.CloudMessaging via NuGet.
csharp
// 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
Lesson 29 of 30Device Features
← Previous Next Lesson →