如何使用春季启动在Google Firebase FCM上推送通知?



我正在寻找一种解决方案,将通知从服务器(Spring Hibernate RESTful API)推送到客户端应用程序,用Angular和Android编码。我想实施一个不会占用用户太多带宽的解决方案,因为我的用户群的连接有限(低带宽和 3G/2G 移动网络)。这排除了轮询作为可能的解决方案。接下来,我研究了套接字连接,但看起来套接字连接可以保持打开状态并导致内存泄漏问题。接下来,我正在尝试谷歌FCM,但卡在实现中。具体来说,我正在尝试在 FCM 上发送通知,但在客户端没有收到通知。 回顾一下,我必须从JAVA层(Spring boot)发送通知,并在angular和Android上接收它。我已经尝试了一些示例代码来使用 FCM 完成这项工作。但它不起作用。请向我建议解决方案或不同的方法来完成这项工作。 这是我正在使用的代码:

JSONObject body = new JSONObject();
body.put("to", "/users/" + TOPIC);
body.put("priority", "high");
JSONObject notification = new JSONObject();
notification.put("title", "JSA Notification");
notification.put("body", "Happy Message!");
JSONObject data = new JSONObject();
data.put("Key-1", "JSA Data 1");
data.put("Key-2", "JSA Data 2");
body.put("notification", notification);
body.put("data", data);
HttpEntity<String> entity = new HttpEntity<>(body.toString());
RestTemplate restTemplate = new RestTemplate();
ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(new HeaderRequestInterceptor("Authorization", "key=" + FIREBASE_SERVER_KEY));
interceptors.add(new HeaderRequestInterceptor("Content-Type", "application/json"));
restTemplate.setInterceptors(interceptors);
String firebaseResponse = restTemplate.postForObject(FIREBASE_API_URL, entity, String.class);
CompletableFuture<String> pushNotification = CompletableFuture.completedFuture(firebaseResponse);
CompletableFuture.allOf(pushNotification).join();
try {
String firebaseResponse = pushNotification.get();
System.out.println("reponse "+firebaseResponse);
return new ResponseEntity<>(firebaseResponse, HttpStatus.OK);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}

从FirebaseResponse中,我在浏览器中得到以下响应:

{"multicast_id":6197426474050349577,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

看起来我的消息没有发送。 任何示例工作代码将不胜感激。 感谢您的帮助。 - 普拉纳夫

这个问题有一个公认的答案。但对于那些在 2020 年阅读本文的人来说,有一种更简单、更简洁的方法可以使用适用于 Java 的 firebase 管理员 sdk 进行 Firebase 推送通知。

<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>7.0.0</version>
</dependency>

将上述内容添加到您的项目中后,您只需从 FCM 管理控制台生成service-account.json并发送如下所示的通知即可。


GoogleCredentials googleCredentials = GoogleCredentials
.fromStream(new ClassPathResource("firebase-service-account.json").getInputStream());
FirebaseOptions firebaseOptions = FirebaseOptions
.builder()
.setCredentials(googleCredentials)
.build();
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "my-app");
FirebaseMessaging firebaseMessaging = FirebaseMessaging.getInstance(app);
Notification notification = Notification
.builder()
.setTitle(note.getSubject())
.setBody(note.getContent())
.build();
Message message = Message
.builder()
.setToken(token)            //this can be setTopic() too
.setNotification(notification)
.putAllData(note.getData())
.build();
return firebaseMessaging.send(message);

我有一篇关于如何在 Spring 引导项目中实现这一点的详细文章 这里.

https://springhow.com/spring-boot-firebase-push-notification/

FIREBASE_SERVER_KEY是错误的。如果FIREBASE_SERVER_KEY是正确的,那么响应应该在下面。

{"multicast_id":7163645588662808791,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1563534037236589%eec6c0c0eec6c0c0"}]}

最新更新