Ad
How To Get The Child Of The Ref In Cloud Messaging Android?
I am making an android app to send and receive notification using cloud messaging I've tried this to get the child of the ref but none of them worked for me.
I've tried this:
const beforeData = change.before.val(); // data before the write
const afterData = change.after.val(); // data after the write
//get the user id of the person who sent the message
const senderIdbefore = beforeData.val()
const senderIdafter = afterData.val()
const userid = senderIdbefore.user_id
And this:
const senderId = change.ref.parent.child('user_id').val();
but none of them worked for me.
Here is the code that I am working on:
const functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification =
functions.database.ref('/messages/{userId}/{messageId}').onWrite((change,
context) => {
//get the userId of the person receiving the notification because we need
to get their token
const receiverId = context.params.userId;
console.log("receiverId: ", receiverId);
const beforeData = change.before.val(); // data before the write
const afterData = change.after.val(); // data after the write
//get the user id of the person who sent the message
const senderIdbefore = beforeData.val()
const senderIdafter = afterData.val()
const userid = senderIdbefore.user_id
//
console.log("senderId: ", senderIdbefore);
console.log("senderId: ", senderIdafter);
console.log("senderId: ", user_id);
//get the message
const message = change.data.child('message').val();
console.log("message: ", message);
//get the message id. We'll be sending this in the payload
const messageId = context.params.messageId;
console.log("messageId: ", messageId);
//query the users node and get the name of the user who sent the message
return admin.database().ref("/users/" + senderId).once('value').then((snap,context) => {
const senderName = context.child("name").val();
console.log("senderName: ", senderName);
//get the token of the user receiving the message
return admin.database().ref("/users/" + receiverId).once('value').then((snap,context) => {
const token = context.child("messaging_token").val();
console.log("token: ", token);
//we have everything we need
//Build the message payload and send the message
console.log("Construction the notification message.");
const payload = {
data: {
data_type: "direct_message",
title: "New Message from " + senderName,
message: message,
message_id: messageId,
}
};
return admin.messaging().sendToDevice(token, payload)
.then(function(response) {
console.log("Successfully sent message:", response);
return null
})
.catch(function(error) {
console.log("Error sending message:", error);
});
});
});
});
some of the errors I got:
TypeError: Cannot read property 'child' of undefined
at exports.sendNotification.functions.database.ref.onWrite
(/srv/index.js:14:32)
TypeError: Cannot read property 'val' of null
at exports.sendNotification.functions.database.ref.onWrite
(/srv/index.js:17:36)
Ad
Answer
Though I am not much of a web developer but I wrote some cloud functions in few of my apps. So what I see wrong here is (snap,context)
is not the signature of once()
(I am not completely sure). Maybe that's the reason it is getting null value. You should change it to snap
(or whatever, say snapshot) and context.child("name").val()
to snap.child("name").val()
etc and see if it works.
Also, I wrote a similar script for my chatting app, you can check it here
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad