[英]Push Notification using Firebase and Google Cloud Messaging
[英]Push Notification Using Google Cloud Functions and Google Firebase
我有一個使用Firebase的android 客戶端應用程序和管理應用程序 。 每當用戶在客戶端應用程序中注冊時,我都需要向管理應用發送推送通知。 我正在嘗試將Cloud Functions for Firebase
。 我已經導出了該功能,我也可以在Firebase控制台上看到它。
這是我的index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendMessageToAdmin = functions.database.ref('/tokens/users/{userId}/{regToken}').onWrite(event => {
if (event.data.previous.exists()) {
return;
}
const userId = event.params.userId;
const regToken = event.params.regToken;
// Notification details.
const payload = {
notification: {
title: 'You have a new User.',
body: `${userId} is the id.`,
}
};
return admin.messaging().sendToDevice(regToken, payload);
});
這是我在firebase的數據庫結構:
如果我使用任何在線門戶網站發送推送,甚至使用FCM將推送發送到管理應用程序進行測試,我都將收到推送。 但是此Cloud Function並未發送推送。 有人可以指導我我在做什么錯。
編輯
如果我將功能更改為以下內容,那么它將起作用。 但是我仍然想知道為什么上面的功能不起作用。
exports.sendMessageToAdmin = functions.database.ref('/tokens/users/{userId}').onWrite(event => {
if (event.data.previous.exists()) {
return;
}
const userId = event.params.userId;
var eventSnapshot = event.data;
const regToken = eventSnapshot.child("regToken").val();
Notification details.
const payload = {
notification: {
title: 'You have a new User.',
body: `${userId} is the id.`,
}
};
return admin.messaging().sendToDevice(regToken, payload);
});
在原始代碼中,您具有:
const regToken = event.params.regToken;
event.params.regToken
不返回的值regToken
它返回的值通配符路徑段的參考。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.