如何使用 google api 向多个设备发送 FCM 推送通知?

Posted

技术标签:

【中文标题】如何使用 google api 向多个设备发送 FCM 推送通知?【英文标题】:How to send FCM push notification to multiple devices using google apis? 【发布时间】:2019-10-01 10:27:41 【问题描述】:

我想一次向多个设备发送推送通知。我可以传递令牌数组吗? 可以使用google api吗?请帮我解决这个问题。

request(
  
    method: 'POST',
    uri:
      'https://fcm.googleapis.com/v1/projects/projectId/messages:send',
    headers: 
      'content-type': 'application/json',
      Authorization: `Bearer $token`,
      'apns-priority': 'high',
      content_available: true
    ,
    body: 
      message: 
        token: token,
        notification: 
          body: 'This is an FCM notification message!',
          title: 'FCM Title'
        ,
        android: 
          notification: 
            sound: 'default'
          
        
      
    ,
    json: true
  ,
  async function(error, response, body) 
    if (error) 
      console.log(error);
    
    console.log(body);
  
);

【问题讨论】:

【参考方案1】:

如果您想自己发出 POST 请求,您可以选择:

1) 使用旧的 HTTP 服务器协议。这样,您将能够使用“registration_ids”字段,该字段需要 POST 正文中的令牌字符串数组。 Docs here.

2) 坚持您现在正在使用的 HTTP v1 API,但由于“token”字段只需要字符串,您可以首先为您的用户订阅特定主题并在 POST 的“topic”字段中使用该主题身体。

此外,您可能更喜欢使用 Admin SDK 来发送批量通知。您可以进一步检查here。

【讨论】:

以上是关于如何使用 google api 向多个设备发送 FCM 推送通知?的主要内容,如果未能解决你的问题,请参考以下文章

如何将 Chrome 推送通知发送到多个设备

如何使用 GCM 和 java 向多个设备发送推送通知

如何在 iOS 中使用 Google Firebase 向特定用户发送推送通知?

无法通过 gmail api 向多个发件人发送电子邮件

多个通知是从(GCM)Google 通知服务器获取的?

如何使用 node-fcm 向多个设备发送通知