我正在尝试为我的移动应用设置推送通知,我有以下FireBase数据库,我想要做的是当isOpen值发生变化时,我需要得到它的父母和大父母&# 39;发送适当通知的密钥。可以在红色框中看到。我可以得到父母的钥匙,但我无法得到父母的钥匙。我目前的代码就是我从https://github.com/firebase/functions-samples/tree/master/fcm-notifications
获取的代码任何帮助?
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
/**
* Triggers when a user gets a new follower and sends a notification.
*
* Followers add a flag to `/followers/{followedUid}/{followerUid}`.
* Users save their device notification tokens to `/users/{followedUid}/notificationTokens/{notificationToken}`.
*/
exports.sendUserNotification = functions.database.ref('/devices').onUpdate( event => {
console.log("event.data: ", event.data);
console.log("event.data.toJSON(): ", event.data.toJSON());
const userUid = event.auth.variable.user_id;
const deviceUid = event.params.deviceUid;
event.data.adminRef.on('child_changed', function(snapshot){
console.log("snapshot: ", snapshot);
console.log("snapshot.val(): ", snapshot.val());
console.log("snapshot.key:", snapshot.key);
//snapshot.key: 1f2d34d
console.log("snapshot.ref.parent.key:", snapshot.ref.parent.key);
//snapshot.ref.parent.key: devices
const deviceId = snapshot.key;
//const userUid = event.params.userUid;
// If un-follow we exit the function.
// console.log("itemSnapshot: ", itemSnapshot);
if (!event.data.val()) {
return console.log('User ', userUid, 'has device ', deviceUid);
}
console.log('We have a new user UID:', userUid, 'for device:', deviceUid);
// Get the list of device notification tokens.
const getDeviceTokensPromise = admin.database().ref(`/users/${userUid}/pushToken`).once('value');
// Get the follower profile.
const getUserProfilePromise = admin.auth().getUser(userUid);
const getDeviceInformation = admin.database().ref('/devices/${deviceUid}/')
return Promise.all([getDeviceTokensPromise, getUserProfilePromise]).then(results => {
const tokensSnapshot = results[0];
const user = results[1];
// Check if there are any device tokens.
if (!tokensSnapshot.hasChildren()) {
return console.log('There are no notification tokens to send to.');
}
console.log('There are', tokensSnapshot.numChildren(), 'tokens to send notifications to.');
console.log('Fetched follower profile', follower);
// Notification details.
const payload = {
notification: {
title: 'Device Informatiın',
body: `${follower.displayName} is now following you.`,
icon: follower.photoURL
}
};
// Listing all tokens.
const tokens = Object.keys(tokensSnapshot.val());
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload).then(response => {
// For each message check if there was an error.
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', tokens[index], error);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
return Promise.all(tokensToRemove);
});
});
});
});
答案 0 :(得分:1)
答案很延迟。获取父母的钥匙:
snap.ref.parent.key;