首页 文章

firebase函数通知

提问于
浏览
2

我正在尝试使用FCM服务发送简单的推送通知 .

每次调用我的函数时,我都希望收到一个通知,我希望我的手机振铃并振动 . 我有两个问题:

当我收到通知时,我得到5个而不是1个 .

第二个问题是,如果我收到通知后手机的声音被激活,手机就不会振动或响铃,但如果是振动模式,它会在我收到通知时振动 .

任何想法如何解决这个问题?谢谢!

'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仪表板发送通知 . 即使在高级设置下我选择了声音,当我在手机上收到通知时,它也没有振动或振铃 .

1 回答

  • 1

    问题是在我的手机上我的通知声音已关闭 . 我在我的android / res / raw中添加了声音"mysound"并将 sound:"default" 更改为 sound:"mysound" ,现在我收到通知后我甚至得到了我想要的声音 .

相关问题