Push notification in .NET MAUI (Android)

Understanding Push Notifications: Push notifications are messages that are delivered to a user's device from a server or a cloud service. These notifications appear on the device's notification tray and can contain text, images, or even actions for the user to take. They serve as a direct channel of communication between the app and the user, enabling real-time updates, reminders, and user engagement.

Setting Up Push Notifications in .NET MAUI for Android:

    To enable push notifications in your .NET MAUI app for Android, you need to follow a series of steps:

    Step 1. Configure Firebase Cloud Messaging (FCM):
        - Create a new project on the Firebase console (https://console.firebase.google.com) and associate it with your Android app.
        - Download the google-services.json configuration file and add it to Platforms folder in your .NET MAUI project.
        

        Right click on 
google-services.json, then choose properties, then select build action to GoogleServicesJson.

    Step 2. Install Required NuGet Packages:

    Step 3. Handle Push Notification:

        - Implement the necessary logic to handle received push notifications in your .NET MAUI app. This typically involves registering a service that extends FirebaseMessagingService and overriding the appropriate methods to handle incoming notifications.

        - Override OnNewToken method to get a token and save it in Preferences

Important a token is only generated once when your app is installed or you clear data on app, so that's why you need to save it into storage.

        - Override OnMessageReceived to handle incoming notification from Firebase

        - Create push notification channel to display notification on device.

        in MainActivity.cs add following code:

        - Create FireBaseService.cs in the same folder with MainActivity.cs.

    Step 4. Send Push Notification:
        - Go to https://console.firebase.google.com/, select your project
        - Click on Create your first campaign button, then select Firebase Notification messages option. 
        - Add your device token you received in OnNewToken method.
        - There are some images you can follow:




    Fortunately, Google provide us Apis to send a push notification. You can follow the code below.

        - You can get a Server Key here:
        


    That's the whole thing you need to setup push notification for Android in .NET MAUI. Hope this helps.

    See handle receiving of push notifications for Android to handle when a user taps on a notification from the notification tray.