Firebase 消息 sendToDevice() 新 API?

问题描述 投票:0回答:2

用于 Firebase 消息传递的“旧/版本 8”JavaScript API 具有向移动设备发送通知的功能:

admin.messaging().sendToDevice(...)

“新/模块化/版本 9”JavaScript API 使用

getMessaging(app)
获取消息传递对象,但该对象似乎没有任何功能(无论是实际的还是记录的)来执行等效操作。我在新 API 中找不到类似的函数,甚至在“messaging-compat”中也找不到,它应该在新 API 中模拟旧 API。难道只是不存在吗?我是否必须完全放弃新的 API 才能获得这一功能?或者也许此功能已转移到其他 API?

如果重要的话,我正在尝试在 Firebase 云功能中执行此操作。

node.js firebase-cloud-messaging firebase-admin
2个回答
2
投票

据我所知,您只需将令牌传递到传递给

to
的 JSON 对象的
send
属性中,如有关 向特定设备发送消息的文档中的代码所示:

// This registration token comes from the client FCM SDKs.
const registrationToken = 'YOUR_REGISTRATION_TOKEN';

const message = {
  data: {
    score: '850',
    time: '2:45'
  },
  token: registrationToken
};

// Send a message to the device corresponding to the provided
// registration token.
getMessaging().send(message)

0
投票

添加@frank-van-puffelen 的答案。看起来发送到多播就像替换为

sendToDevice(registrationTokenOrTokens: string | string[], payload: MessagingPayload, options?: MessagingOptions): Promise<MessagingDevicesResponse>;

常量注册令牌 = [ 'YOUR_REGISTRATION_TOKEN_1', // … 'YOUR_REGISTRATION_TOKEN_N', ];

const message = {
  data: {score: '850', time: '2:45'},
  tokens: registrationTokens,
};

getMessaging().sendMulticast(message)
  .then((response) => {
    console.log(response.successCount + ' messages were sent successfully');
  });
© www.soinside.com 2019 - 2024. All rights reserved.