1

I am using firebase database to store my iOS app data.

enter image description here

I am saving user tracking data in this app which is working fine.

I have to send a push notification to the user (userID = 57411405) using push token which I am saving in 'IOS' field.

This cloud function I am using :

This cloud function is working fine. I am able to track event which save new tracking data. Here is log of this cloud function:

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

// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions

exports.databasechanges = functions.database.ref('/users/{id}/LocationTracking').onWrite(event =>{

var eventSnapshot = event.data

console.log('UserId - ', event.params.id);

const userID = event.params.id
const root = event.data.ref.root

admin.database().ref('users/{id}/NotificationToken').on('value').then((snapshot) => {

  var token = snapshot.val().IOS;

  console.log('token',token)

  return snapshot.val();
}).catch(error => {
  console.error(error);
  res.error(500);
});

return eventSnapshot.val()

});

But on cloud function console I am getting this error:

enter image description here Now, I am not able to figure out how to access this push token (IOS) and send push notification using cloud function.

2
  • Please don't show images of code. It's far better to copy the code into the question into a code block so it's easier to read and search. Commented Mar 12, 2018 at 6:57
  • @DougStevenson I edited my question with more details. Commented Mar 12, 2018 at 7:40

1 Answer 1

4

To get the field "IOS", try this:

//inside the trigger function
admin.database().ref('/users/'+event.params.id+'/NotificationToken/IOS').once('v‌​alue').then((snapshot) => { 
var token=snapshot.val().IOS;

 });
Sign up to request clarification or add additional context in comments.

5 Comments

I am getting this error: 24:1 error Expected catch() or return promise/catch-or-return
Cloud function has deployed successfully but I am getting error in cloud function console. I edited my question please have a look.
@ajeetsharma use once() instead of on() and get the value once
it's not working :( . getting this error in console: TypeError: Cannot read property 'IOS' of null at admin.database.ref.once.then (/user_code/index.js:34:29) at process._tickDomainCallback (internal/process/next_tick.js:135:7)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.