You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FCM旧API迁移至HTTP v1时出现google未定义错误求助

FCM推送迁移至V1 API后报错"google is not defined"的问题

我们向Android设备发送FCM推送通知的服务已失效,原因是:

使用旧版API发送消息(包括上行消息)已于2023年6月20日弃用,并于2024年7月22日开始停止服务

旧版FCM推送函数

function sendPaidNotificationMessage(to, transactionID, mobileNumber, callback) {
    unirest.post('https://fcm.googleapis.com/fcm/send').headers({
        'Authorization': config.get('googleNotificationAuth'),
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'cache-control': 'no-cache'
    }).proxy('http://xxxxxxxx:xxxx').send({
        'to': to,
        'priority': 'high',
        'restricted_package_name': config.get('googlePkgName'),
        'notification': {
            'title': 'Hello. Invoice # ' + transactionID + ' has been paid. Thank you.'
        },
        'data': {
            'transactionID': transactionID,
            'mobileNumber': mobileNumber
        }
    }).end(function(responseFCM) {
        callback(responseFCM);
    });
}

迁移至V1 API后的推送函数

function sendPaidNotificationMessage(to, transactionID, mobileNumber, callback) {
    var myAccessToken = getAccessToken()
    unirest.post('https://fcm.googleapis.com/v1/projects/xxx-xxxx-xxxx-xxxx/messages:send').headers({
        'Authorization': myAccessToken,
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'cache-control': 'no-cache'
    }).proxy('http://xxxxxxxx:xxxx').send({
        'to': to,
        'priority': 'high',
        'restricted_package_name': config.get('googlePkgName'),
        'notification': {
            'title': 'Hello. Invoice # ' + transactionID + ' has been paid. Thank you.'
        },
        'data': {
            'transactionID': transactionID,
            'mobileNumber': mobileNumber
        }
    }).end(function(responseFCM) {
        callback(responseFCM);
    });
}

// 参考FCM迁移文档实现的获取AccessToken函数
function getAccessToken() {
  return new Promise(function(resolve, reject) {
    const key = require('./service-account.json');
    const jwtClient = new google.auth.JWT(
      key.client_email,
      null,
      key.private_key,
      SCOPES,
      null
    );
    jwtClient.authorize(function(err, tokens) {
      if (err) {
        reject(err);
        return;
      }
      resolve(tokens.access_token);
    });
  });
}

运行环境与报错

运行环境:FreeBSD 13.2、Node.js 18.16.0

报错信息:

error: uncaughtException: google is not defined date=Tue Sep 17 2024
00:33:45 GMT+0000 (Coordinated Universal Time), pid=47520, uid=0,
gid=0, cwd=/storage/app/platform_xxxx/xxxx.xxxx.notification,
execPath=/usr/local/bin/node, version=v18.16.0,
argv=[/usr/local/bin/node,
/storage/app/platform_xxxx/xxxx.xxxx.notification/server.js],
rss=129961984, heapTotal=54124544, heapUsed=49451000,
external=18824867, arrayBuffers=17895641, loadavg=[0.1279296875,
0.3095703125, 0.2421875], uptime=8452173, trace=[column=22, file=/storage/app/platform_xxxx/xxxxs.xxxx.xxxx/server.js,
function=null, line=1659, method=null, native=false, column=null,
file=null, function=new Promise, line=null, method=null, native=false,
column=9, file=/storage/app/platform_xxxx/xxxx.xxxx.xxxx/server.js,
function=getAccessToken, line=1657, method=null, native=false,
column=22, file=/storage/app/platform_xxxx/xxxx.xxxx.xxxx/server.js,

已尝试的解决

我们已安装google-auth-library依赖:

npm install google-auth-library

但错误依旧,请问问题出在哪里?


内容的提问来源于stack exchange,提问作者anta40

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 08:51:09