首页 > 解决方案 > 如何使用 google apis 向多个设备发送 FCM 推送通知?

问题描述

我想一次向多个设备发送推送通知。我可以传递令牌数组吗?可以使用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.jspush-notificationfirebase-cloud-messaginggoogle-cloud-messaginggoogle-api-client

解决方案


If you want to make the POST requests by yourself, you can prefer to:

1) Use the legacy HTTP server protocol. With that you will be able to use the "registration_ids" field which expects an array of token strings in POST body. Docs here.

2) Stick to HTTP v1 API that you're using right now, but since "token" field is expecting only string, you can first subscribe your users to a specific topic and use that topic in "topic" field in POST body.

Additionally, you may prefer to use Admin SDK to send batch notifications. You can check for further here.


推荐阅读