0

I am relatively new to Android Dev't and have a problem. I tried implementing firebase functions to send notifications to my android application when certain data was saved to my firebase realtime database. But after the data is saved and function is executed, it says in the logs that it is successfully sent but I don't receive any notification pop up on my android test device. What could be the problem? Below is my code.

My JS code.

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

exports.sendNotification = functions.database.ref('/Lecture_Materials/{MIS}/{MISId}/name')
.onWrite(( change,context) =>{

// Grab the current value of what was written to the Realtime Database.
var eventSnapshot = change.after.val();
var str1 = "Lecture material uploaded is: " + eventSnapshot.name;
console.log(eventSnapshot);

var topic = "Management.Information.System";
var payload = {
    data: {
        name: str1,
    }
};

// Send a message to devices subscribed to the provided topic.
return admin.messaging().sendToTopic(topic, payload)
    .then(function (response) {
        // See the MessagingTopicResponse reference documentation for the
        // contents of response.
        console.log("Successfully sent message:", response);
        return;
    })
    .catch(function (error) {
        console.log("Error sending message:", error);
    });
    });

My OnMessageReceived

package com.dreamlazerstudios.gtuconline;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        showNotification(remoteMessage.getData().get("name"));
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {

    }
}

private void showNotification(String name) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("Lecture note uploaded is: " + name)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentText("Lecture Notes")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}

Any help is deeply appreciated.

1 Answers1

0

I think you are trying to send push notification, so you need to do some necessary implementations for firebase cloud messaging. Also in server side, you need push notification key for your application to send notification.

You can check the following documentation to see how to implement Firebase Cloud Messaging in client side (Also you can use Firebase assistant tool in Android Studio, you can find it in Tools>Firebase):

https://firebase.google.com/docs/cloud-messaging/android/client

For the server side, to send notification you can follow the below guide:

https://firebase.google.com/docs/cloud-messaging/admin/send-messages

In addition, you can also use some 3rd party libraries like:

https://www.npmjs.com/package/fcm-push