I want to send the user a push notification with cloud functions on Firebase if someone commented to a topic on his iOS Device. This code below works fine if I put the content of the notification manually.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.Push = functions.database.ref('/placeID/{pushId}/')
.onCreate((snapshot, context) => {
var topic = 'weather';
const payload = {
notification: {
title: 'User Max',
body: 'Hi how are you?',
badge: '1',
sound: 'default'
}
};
admin.messaging().sendToTopic(topic,payload);
})
In the next step I want to get the content of the comment the person has sent. Kinda get the value of the snapshot. I tried it like this and get no error in the Logs Explorer from Google. But also no Push Notification anymore.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.Push = functions.database.ref('/placeID/{autogeneratedplaceID}/')
.onCreate((snapshot, context) => {
var username = snapshot.child("userName").val()
var usercomment = snapshot.child("userComment").val()
var topic = 'weather';
const payload = {
notification: {
title: username,
body: usercomment,
badge: '1',
sound: 'default'
}
};
admin.messaging().sendToTopic(topic,payload);
})
What is wrong here? I also found a video on Youtube with the exact solution. But this is for android and it did not really help me. Youtube
This is how my firebase realtime database structure looks like.
