Posted on

Android java app with notification from mvc using firebase cloud messaging – part 3

Notification service in Android

Now we need to create in Android the service able to manage the notification incoming from firebase and to show these to user. Follow this usefull article Working easily with FCM push notifications in Android. You’ll have a class like this.

package com.mycompany.myfirstapp;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;



//is the class that we can find in the manifest as a service

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    public static final String TAG = "MsgFirebaseServ";


    public void onMessageReceived(RemoteMessage remoteMessage) {

        String title = "";
        String body = "";
        String objectId = "";
        String objectType = "";

        if (remoteMessage.getData() != null) {
            title = remoteMessage.getData().get("title");
            body = remoteMessage.getData().get("body");
            objectId = remoteMessage.getData().get("object_id");
            objectType = remoteMessage.getData().get("objectType");

        Notification notification = new NotificationCompat.Builder(this)
        NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
        manager.notify(/*notification id*/0, notification);

Run your app on your device

Follow this instruction :

First test using your app in your device

  • Run the app on your device or emulator. The app should had created a new register id for the device and send it to mvc site. Now the device is registered.
  • Open Firebase console, go down in the menu on the left. Open Cloud Messaging session : you should find the possibility to send notification to your device starting from this page. So you can test correct developing of above class.

Send notification from mvc to firebase

For backend developers. You should create a class to manage notification to firebase. Follow the article Firebase push notifications using a .NET Backend to prepare a class to manage the notification to Firebase.

Then prepare your notification test task in a controller. Something like this :

        public Task<bool> NoteAsync()
            var device = db.RegisteredDevices.Where(d => d.ENABLED == true);
            string[] da = new string[device.Count()];
            int i = 0;
            foreach (RegDevice r in device)
                da[i] = r.ID;
            return FCMPushNotification.SendPushNotification(da,serverkey,"Title","body");

Firebase messaging, where to get Server Key?

Click the Settings (Cog wheel) icon next to your project name at the top of the new Firebase Console, as per screenshot below:

  1. Click Project settings.
  2. Click on the Cloud Messaging tab.

Final test

Call the mvc method created. You should see a notification in your device.

Android java app with notification from mvc using firebase cloud messaging – part 1

Android java app with notification from mvc using firebase cloud messaging – part 2