java - Firebase 云消息传递休息 API Spring

标签 java spring-mvc firebase spring-boot firebase-cloud-messaging

我必须在 Spring Java 中为多层架构创建一个 Rest API,其中需要为 Firebase 云消息传递 (FCM) 构建 DAO、 Controller 、服务管理器以将推送通知消息发送到 Android 应用程序,但我可以无法在 Java 中配置服务器以向设备发送通知。 我怎么能?

最佳答案

您可以通过以下方式实现此目的:

第 1 步: 在 firebase 上创建项目并生成服务器 key 。

第 2 步: 为 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"
}

第 3 步: 编写一个 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)然后接收者将收到推送消息

谢谢:)

关于java - Firebase 云消息传递休息 API Spring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46705608/

相关文章:

javascript - 我需要从 $scope 到其他数据库的不同 id 中获取最大值

java - 使用扫描仪读取单个字符

java - 何时使用 Spring @Transactional (propagation = Propagation.SUPPORTS)?

java - JMeter 中的第一个 HTTP 请求花费了很长时间

java - Spring MVC 和 UI 组件

java - Spring MVC - 从 JSP 中的列表访问表单中的对象

java - 向 ArrayList 添加元素失败

java - Spring 异常处理程序 - 使用带注释和 xml 定义

java - 如何在 Firebase 中使用推送通知关闭 Activity ?

android - 在 Jetpack Compose 不可组合函数中获取上下文