【发布时间】:2018-05-21 12:36:03
【问题描述】:
我不知道如何从我要发送的 index.js 中的 Firebase 云功能向设备发送不同类型的通知(cmets 通知)(如通知)。
我正在使用此代码向设备获取以下通知,但我不知道如何获取其他通知。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/notification/{user_id}/{notification_id}').onWrite((change, context) => {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification to send to : ', user_id);
const fromUser = admin.database().ref(`/notification/${user_id}/${notification_id}`).once('value');
return fromUser.then(fromUserResult => {
const from_user_id = fromUserResult.val().from;
const from_message = fromUserResult.val().message;
console.log('You have new notification from : ', from_user_id);
const userQuery = admin.database().ref(`users/${from_user_id}/username`).once('value');
const deviceToken = admin.database().ref(`users/${user_id}/device_token`).once('value');
return Promise.all([userQuery,deviceToken]).then(result =>{
const userName = result[0].val();
const token_id = result[1].val();
const payload1 = {
notification:{
title: "some is following you",
body: `${userName} is following you`,
icon: "default",
click_action : "alpha.noname_TARGET_NOTFICATION"
},
data:{
from_user_id:from_user_id
}
};
return admin.messaging().sendToDevice(token_id, payload1).then(result=>{
console.log("notification sent");
});
})
.then(response => {
console.log('This was the notification Feature');
return true;
}).catch(error => {
console.log(error);
//response.status(500).send(error);
//any other error treatment
});
});
});
【问题讨论】:
-
这样想。如果您旁边有 3 个按钮,当您单击其中一个按钮时,它会向云发送一个字符串值。它旁边的按钮将执行相同的操作,但具有不同的值。因此,不是硬编码“有人跟着你”,而是插入请求传入的值。例如 request.like_string。
-
你告诉我应该在我的 android studio 项目中创建一个字符串“有人在关注你”并将其传递给服务器以显示该通知
-
我有 click_action ...那怎么样
标签: javascript android node.js push-notification firebase-cloud-messaging