Firebase 功能通知
firebase functions notifications
我正在尝试使用 FCM 服务发送简单的推送通知。
每次调用我的函数时,我都希望收到一条通知,并且我希望 phone 响铃和振动。我有 2 个问题:
当我收到通知时,我收到了 5 个,而不是一个。
第二个问题是,如果我的 phone 的声音在我收到通知时被激活,phone 不会振动或响铃,但如果它处于振动模式,它当我收到通知时振动。
有什么解决办法吗?谢谢!
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/Alerts/{email_encoded}/{alert_id}').onWrite(event => {
const email_encoded = event.params.email_encoded;
const alert_id = event.params.alert_id;
const deviceToken = admin.database().ref(`/Users/${email_encoded}/device_token`).once('value');
deviceToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
title: "Alert",
body: "alert triggered",
icon: "default",
sound:"default",
vibrate:"true"
}
};
return admin.messaging().sendToDevice(token_id, payload);
});
});
和 firebase 数据库 json 结构:
"Alerts" : {
"asdfgh@yahoo,com" : {
"-KtXc_c7RMxysWGEb98L" : {
"bid" : "1.30",
"email" : "asdfgh@yahoo.com",
"id" : "-KtXc_c7RMxysWGEb98L",
"parity" : "EURUSD",
"sign" : ">"
},
"-KtmCD7REa9AKK57xhXP" : {
"bid" : "1.33",
"email" : "asdfgh@yahoo.com",
"id" : "-KtmCD7REa9AKK57xhXP",
"parity" : "EURUSD",
"sign" : ">"
},
"-KtmCpypQM_N1ub471Ta" : {
"bid" : "1.333",
"email" : "asdfgh@yahoo.com",
"id" : "-KtmCpypQM_N1ub471Ta",
"parity" : "EURUSD",
"sign" : ">"
}
}
我尝试从 firebase 仪表板发送通知。即使在高级设置中我选择了启用声音,当我在 phone 上收到通知时,它也没有振动或响铃。
问题是我的 phone 上的通知声音已关闭。我在我的 android /res/raw 中添加了一个声音 "mysound" 并将 sound:"default"
更改为 sound:"mysound"
现在我什至在收到通知时得到了我想要的声音。
我正在尝试使用 FCM 服务发送简单的推送通知。
每次调用我的函数时,我都希望收到一条通知,并且我希望 phone 响铃和振动。我有 2 个问题:
当我收到通知时,我收到了 5 个,而不是一个。
第二个问题是,如果我的 phone 的声音在我收到通知时被激活,phone 不会振动或响铃,但如果它处于振动模式,它当我收到通知时振动。
有什么解决办法吗?谢谢!
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/Alerts/{email_encoded}/{alert_id}').onWrite(event => {
const email_encoded = event.params.email_encoded;
const alert_id = event.params.alert_id;
const deviceToken = admin.database().ref(`/Users/${email_encoded}/device_token`).once('value');
deviceToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
title: "Alert",
body: "alert triggered",
icon: "default",
sound:"default",
vibrate:"true"
}
};
return admin.messaging().sendToDevice(token_id, payload);
});
});
和 firebase 数据库 json 结构:
"Alerts" : {
"asdfgh@yahoo,com" : {
"-KtXc_c7RMxysWGEb98L" : {
"bid" : "1.30",
"email" : "asdfgh@yahoo.com",
"id" : "-KtXc_c7RMxysWGEb98L",
"parity" : "EURUSD",
"sign" : ">"
},
"-KtmCD7REa9AKK57xhXP" : {
"bid" : "1.33",
"email" : "asdfgh@yahoo.com",
"id" : "-KtmCD7REa9AKK57xhXP",
"parity" : "EURUSD",
"sign" : ">"
},
"-KtmCpypQM_N1ub471Ta" : {
"bid" : "1.333",
"email" : "asdfgh@yahoo.com",
"id" : "-KtmCpypQM_N1ub471Ta",
"parity" : "EURUSD",
"sign" : ">"
}
}
我尝试从 firebase 仪表板发送通知。即使在高级设置中我选择了启用声音,当我在 phone 上收到通知时,它也没有振动或响铃。
问题是我的 phone 上的通知声音已关闭。我在我的 android /res/raw 中添加了一个声音 "mysound" 并将 sound:"default"
更改为 sound:"mysound"
现在我什至在收到通知时得到了我想要的声音。