【问题标题】:How to send FCM push notification to multiple devices using google apis?如何使用 google api 向多个设备发送 FCM 推送通知?
【发布时间】: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);
  }
);

【问题讨论】:

    标签: node.js push-notification firebase-cloud-messaging google-cloud-messaging google-api-client


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-15
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 2019-10-13
      • 2017-11-08
      • 1970-01-01
      相关资源
      最近更新 更多