【问题标题】:Firebase cloud messaging rest API springFirebase 云消息传递 REST API Spring
【发布时间】:2018-03-24 03:43:29
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    以下是实现此目的的方法:

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

    第 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 caller 服务,它将通过以下 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&lt;String&gt; receiverKeys, String messageTitle, String message),接收方就会收到推送消息

    谢谢:)

    【讨论】:

    • 这完美无瑕。如果需要data AND notification,还是只能选择其中之一?
    • list receiverkeys 的内容是什么?
    • 这里,receiver key 是receiver 的设备fcm key。这个key会在client端吸收fcm后从client端找到。
    • HttpEntity httpEntity = new HttpEntity????>(json.toString(), httpHeaders);新 HttpEntity 的类型是什么,使用时显示 Error:(47, 45) java: cannot infer type arguments for org.springframework.http.HttpEntity
    【解决方案2】:

    在配置您的 FCM 帐户后,@Autowire 在您的 @Component 类中的 FCM。 tutorial

    【讨论】:

    • habil 先生它不起作用,它具有拦截器服务类,该服务类在拦截方法中具有各种库,例如 import org.springframework.http.HttpRequest;
    • 这些库我无法导入。我也无法为这些库找到准确的 jar 文件以便我可以导入
    • 检查一下:link 并且不要忘记添加 spring-boot-starter-web 依赖项。
    猜你喜欢
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 2019-07-12
    相关资源
    最近更新 更多