可以使用 Firebase Admin SDK 将消息发送到一批令牌到 FCM 客户端吗?

Can a message be sent to a batch of tokens to FCM clients using the Firebase Admin SDK?

我想使用令牌向一批设备发送消息,因为我有不同的接收者组,这些组非常动态并且可以更改。我在后端 Spring 引导服务器上定义了我的组,而 Message.Builder 只有一个 "setToken" 方法。这是否意味着我必须遍历所有令牌才能发送消息?

参考:https://firebase.google.com/docs/reference/admin/java/reference/com/google/firebase/messaging/Message.Builder.html

sending messages to multiple devices suggests that you need to use MulticastMessage 的文档。

// Create a list containing up to 100 registration tokens.
// These registration tokens come from the client FCM SDKs.
List<String> registrationTokens = Arrays.asList(
    "YOUR_REGISTRATION_TOKEN_1",
    // ...
    "YOUR_REGISTRATION_TOKEN_n"
);

MulticastMessage message = MulticastMessage.builder()
    .putData("score", "850")
    .putData("time", "2:45")
    .addAllTokens(registrationTokens)
    .build();
BatchResponse response = FirebaseMessaging.getInstance().sendMulticast(message);
// See the BatchResponse reference documentation
// for the contents of response.
System.out.println(response.getSuccessCount() + " messages were sent successfully");