Firebase - 使用 Java 向特定设备发送消息

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

我有这段代码:

        log.info("Sending message to topic: " + topic);

        // This registration token comes from the client FCM SDKs.
        String registrationToken = "AAAAVjsyE68:APA91bHOdV7whbcjOKxohCsw9TBuZaYfvqSiNk6uSpQ-_8THdRjdWepCa-fxrqvJjePwls-eObnD4tzIaYOZYTGAzped3nWI6rer91f1IoLvBf15M4NZEhsdPu-goKHe2zp4nDG";

        // See documentation on defining a message payload.
        Message message = Message.builder()
                .setTopic(topic)
                .setNotification(Notification.builder()
                        .setTitle("Your notification title")
                        .setBody("Your notification message")
                        .build())
                .setToken(registrationToken)
                .build();

// Send a message to the device corresponding to the provided
// registration token.
        String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
        log.info("Successfully sent message: " + response);

但是我有这个错误:

aused by: java.lang.IllegalArgumentException: Exactly one of token, topic or condition must be specified
    at com.google.common.base.Preconditions.checkArgument(Preconditions.java:143)
    at com.google.firebase.messaging.Message.<init>(Message.java:80)
    at com.google.firebase.messaging.Message.<init>(Message.java:40)
    at com.google.firebase.messaging.Message$Builder.build(Message.java:295)
    at com.mysticplanets.service.FirestoreMessageService.sendMessageToTopic(FirestoreMessageService.java:50)
java spring-boot firebase google-cloud-functions firebase-cloud-messaging
1个回答
0
投票

错误提示:

必须指定令牌、主题或条件之一

您的消息构建器指定两者主题和令牌。您必须决定使用哪一个来定位此消息 - 不能两者兼而有之。

消息的 API 文档中重复了这一点:

收件人信息必须仅包含一个令牌、主题或条件参数。

© www.soinside.com 2019 - 2024. All rights reserved.