7

I was following this tutorial from Udacity where Cloud Functions for Firebase was used to change the data of a ref on data added. I wanted to use similar function, but for sending a push notification to users subscribed to topic.

This is the function I am using.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref('/messages/{pushId}/text').onWrite((event) => {
    const data = event.data;
    console.log('Message received');
    if(!data.changed()){
        console.log('Nothing changed');
        return;
    }else{
        console.log(data.val());
    }

    const payLoad = {
        notification:{
            title: 'Message received',
            body: 'You received a new message',
            sound: "default"
        }
    };

    const options = {
        priority: "high",
        timeToLive: 60*60*2
    };

    return admin.messaging().sendToTopic("Message_Notifications", payLoad, options);
});

Looking at the Firebase function log I can see that the function gets called when I add a new value to the database. The I am subscribing to the topic in the mainactivity with

FirebaseMessaging.getInstance().subscribeToTopic("Message_Notification");

And after a few hours now I can see the topic in my topics list in firebase notification console. I send a notification from there and it worked.

What are the other things I need to do to have a notification show up every time a new message is added?

Any suggestion is appreciated. I am fairly new to Firebase. So if there is a better blog/post about it please redirect me so that I can learn more.

Thanks in advance.

AL.
  • 36,815
  • 10
  • 142
  • 281
raymanM
  • 73
  • 1
  • 6

1 Answers1

7

In your Android app, you are subscribing to "Message_Notification" topic.

In your script, you're sending to "Message_Notifications" topic.

Either add an s in your Android app for the name of the topic or remove the excess s from your script.

AL.
  • 36,815
  • 10
  • 142
  • 281
  • I was looking at so many things. Didn't even notice how I missed that one. Thank you for reading it with patience. Need to get my proof reading improved. Cheers. – raymanM Mar 20 '17 at 02:33
  • It happens to a lot of devs. :) Good luck with your app. Cheers! – AL. Mar 20 '17 at 02:35