【问题标题】:Cannot send firebase push notification from lambda无法从 lambda 发送 Firebase 推送通知
【发布时间】:2020-06-02 01:11:11
【问题描述】:

我希望有人能帮我指出我的错误。

我正在尝试使用 HTTP 旧端点通过 lambda 发送 Firebase 推送通知

https://fcm.googleapis.com/fcm/send

我正在遵循以下指南: https://craigrussell.io/2019/03/send-firebase-fcm-push-notification-from-aws-lambda/

这是我的代码:

const authHeader ='key=A****IV';
const deviceToken ='eut4****pm';
  console.log('sending Push notification');
  return new Promise((resolve, reject) => {
    const options = {
      host: 'fcm.googleapis.com',
      path: '/fcm/send',
      method: 'POST',
      headers: {
        'Authorization': authHeader,
        'Content-Type': 'application/json',
      },
    };

    const req = http.request(options, (res) => {
      console.log('success');
      resolve('success');
    });

    req.on('error', (e) => {
      console.log('failuree' + e.message);
      reject(e.message);
    });

    // const reqBody = '{"to":"' + deviceToken + '", "priority" : "high"}';
    const reqBody = '{"to":"' + deviceToken + '", "priority": "high", "notification": {"title": "Test", "body": "Test"}}';
    console.log(reqBody);

    req.write(reqBody);
    req.end();
  });
}; 

在此之后我没有收到任何推送通知。我在这里做错了吗?

【问题讨论】:

    标签: node.js firebase aws-lambda firebase-cloud-messaging


    【解决方案1】:

    我认为关键是你使用了 http 模块。实际上,在我这边使用 https 模块时效果很好。

    var https = require('https');
    

    完整代码如下:

    var https = require('https');
    exports.handler = async(event) => {
    const authHeader = 'key=AA***y';
    const deviceToken = 'fG***-';
    return new Promise((resolve, reject) => {
        const options = {
            host: 'fcm.googleapis.com',
            path: '/fcm/send',
            method: 'POST',
            headers: {
                'Authorization': authHeader,
                'Content-Type': 'application/json',
            },
        };
    
        console.log(options);
        const req = https.request(options, (res) => {
            console.log('success');
            console.log(res.statusCode);
            resolve('success');
        });
    
        req.on('error', (e) => {
            console.log('failuree' + e.message);
            reject(e.message);
        });
    
        // const reqBody = '{"to":"' + deviceToken + '", "priority" : "high"}';
        const reqBody = '{"to":"' + deviceToken + '", "priority": "high", "notification": {"title": "Test", "body": "Test"}}';
        console.log(reqBody);
    
        req.write(reqBody);
        req.end();
    });
    };
    

    更多细节,你可以看到http请求后的res字段,当你使用http时会显示statusCode 403。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-05
      • 2019-11-11
      • 2019-03-18
      • 2018-11-10
      • 2017-11-23
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多