Firebase云消息传递REST API Spring

5

我需要在Spring Java中为多层架构构建DAO、Controller、Service Manager,以便使用Firebase Cloud Messaging (FCM)向Android应用程序发送推送通知消息。但是我无法在Java中配置服务器以向设备发送通知。我该怎么办?

2个回答

5
以下是实现此操作的方法:
第一步:在Firebase上创建项目并生成服务器密钥。
第二步:为FCM服务器生成JSON对象。其中,消息可能包含数据对象和通知对象。还必须具有接收方FCM ID。示例JSON如下:
{
    "notification":
        {
            "notificationType":"Test",
        "title":"Title ",
        "body":"Here is body"
        },
    "data":
        {"notificationType":"Test",
        "title":"Title ",
        "body":"Here is body"
        },
        "to":"dlDQC5OPTbo:APA91bH8A6VuJ1Wl4TCOD1mKT0kcBr2bDZ-X8qdhpBfQNcXZWlFJuBMrQiKL3MGjdY6RbMNCw0NV1UmbU8eooe975vvRmqrvqJvliU54bsiT3pdvGIHypssf7r-4INt17db4KIqW0pbAkhSaIgl1eYjmzIOQxv2NwwwwXg"
}

步骤三:编写一个Rest调用器服务,通过以下url与fcm服务器进行通信:

https://fcm.googleapis.com/fcm/send

下面是示例工作代码:

public class PushNotificationServiceImpl {
    private final String FIREBASE_API_URL = "https://fcm.googleapis.com/fcm/send";
    private final String FIREBASE_SERVER_KEY = "YOUR_SERVER_KEY";


    public void sendPushNotification(List<String> keys, String messageTitle, String message) {


        JSONObject msg = new JSONObject();

        msg.put("title", messageTitle);
        msg.put("body", message);
        msg.put("notificationType", "Test");

        keys.forEach(key -> {
            System.out.println("\nCalling fcm Server >>>>>>>");
            String response = callToFcmServer(msg, key);
            System.out.println("Got response from fcm Server : " + response + "\n\n");
        });

    }

    private String callToFcmServer(JSONObject message, String receiverFcmKey) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Authorization", "key=" + FIREBASE_SERVER_KEY);
        httpHeaders.set("Content-Type", "application/json");

        JSONObject json = new JSONObject();

        json.put("data", message);
        json.put("notification", message);
        json.put("to", receiverFcmKey);

        System.out.println("Sending :" + json.toString());

        HttpEntity<String> httpEntity = new HttpEntity<>(json.toString(), httpHeaders);
        return restTemplate.postForObject(FIREBASE_API_URL, httpEntity, String.class);
    }
}

您只需要调用 sendPushNotification(List<String> receiverKeys, String messageTitle, String message) 方法,接收者就能收到推送消息。
谢谢:)

这个运行得非常完美。如果需要datanotification两者都必须吗?还是只能选择其中一个? - payne
list<String> receiverkeys 的内容是什么? - Andika Ristian Nugraha
这里,接收器密钥是接收器设备的 FCM 密钥。此密钥将在客户端吸收 FCM 后从客户端中找到。 - Md. Sajedul Karim
1
HttpEntity<String> httpEntity = new HttpEntity<String>(json.toString(), httpHeaders); 当使用它时,新的HttpEntity类型是什么,在这种情况下,它会提示错误(47, 45) java: cannot infer type arguments for org.springframework.http.HttpEntity<>。 - GhostDede

1

在配置FCM账户后,在您的@Component类中使用@Autowire FCM。教程


先生,它不起作用了。它有拦截器服务类,其中的intercept方法包含各种库,例如import org.springframework.http.HttpRequest; - leo
这些库我无法导入。我也无法找到准确的JAR文件,以便我可以导入它们。 - leo
1
看这个:链接,别忘了添加spring-boot-starter-web依赖。 - Habil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接