Tin nhắn đám mây Firebase
Firebase Cloud Messaging (FCM) là một giải pháp nhắn tin đa nền tảng cho phép bạn gửi tin nhắn một cách đáng tin cậy mà không mất phí. Sử dụng FCM, bạn có thể thông báo cho ứng dụng khách rằng email mới hoặc dữ liệu khác có sẵn để đồng bộ hóa. Bạn có thể gửi tin nhắn thông báo để thúc đẩy sự tham gia và duy trì của người dùng. Đối với các trường hợp sử dụng như nhắn tin tức thì, tin nhắn có thể chuyển tải trọng lên tới 4KB cho ứng dụng khách. Đọc chi tiết .
Thông báo đẩy bằng FCM
Bây giờ chúng tôi sẽ xây dựng một ứng dụng thông báo đẩy FCM bằng Spring Boot. Đầu tiên chúng ta cần tạo khóa quản trị SDK Firebase.
Khóa quản trị SDK Firebase
Đã đến bảng điều khiển Firbase: https://console.firebase.google.com/u/0/
- Tạo một dự án mới
- Tạo khóa riêng mới ( Cài đặt dự án -> Tài khoản dịch vụ -> Tạo khóa riêng mới )
Ứng dụng khởi động mùa xuân
Hãy tạo một dự án Spring Boot mới. Đây là cấu trúc dự án của chúng tôi
Đặt tệp JSON SDK của Firebase Admin được tạo trong dự án src / main / resource . Đây là application.properations.
1 2 | app.firebase-config=fcm-test-62dad-firebase-adminsdk-3pd7w-bdaed458f4.json |
Chúng ta cần thêm các phụ thuộc Firebase trong pom.xml .
1 2 3 4 5 6 | <dependency> <groupId>com.google.firebase</groupId> <artifactId>firebase-admin</artifactId> <version>6.9.0</version> </dependency> |
khởi tạo ứng dụng Firebase:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @Value("${app.firebase-config}") private String firebaseConfig; private FirebaseApp firebaseApp; @PostConstruct private void initialize() { try { FirebaseOptions options = new FirebaseOptions.Builder() .setCredentials(GoogleCredentials.fromStream(new ClassPathResource(firebaseConfig).getInputStream())).build(); if (FirebaseApp.getApps().isEmpty()) { this.firebaseApp = FirebaseApp.initializeApp(options); } else { this.firebaseApp = FirebaseApp.getInstance(); } } catch (IOException e) { log.error("Create FirebaseApp Error", e); } } |
Thông báo đẩy tới mã thông báo thiết bị
Để gửi thông báo đẩy tới mã thông báo thiết bị, chúng tôi chỉ cần mã thông báo thiết bị và dữ liệu thông báo.
Điều khiển:
1 2 3 4 5 | @PostMapping("/token") public String sendPnsToDevice(@RequestBody NotificationRequestDto notificationRequestDto) { return notificationService.sendPnsToDevice(notificationRequestDto); } |
ĐẾN:
1 2 3 4 5 6 7 8 | @Data public class NotificationRequestDto { private String target; private String title; private String body; } |
Dịch vụ:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public String sendPnsToDevice(NotificationRequestDto notificationRequestDto) { Message message = Message.builder() .setToken(notificationRequestDto.getTarget()) .setNotification(new Notification(notificationRequestDto.getTitle(), notificationRequestDto.getBody())) .putData("content", notificationRequestDto.getTitle()) .putData("body", notificationRequestDto.getBody()) .build(); String response = null; try { response = FirebaseMessaging.getInstance().send(message); } catch (FirebaseMessagingException e) { log.error("Fail to send firebase notification", e); } return response; } |
Đẩy thông báo đến chủ đề
Để gửi thông báo đẩy đến chủ đề, đầu tiên chúng ta cần đăng ký chủ đề bằng mã thông báo thiết bị. Vì vậy, quá trình sẽ được đăng ký một chủ đề bằng mã thông báo thiết bị, gửi thông báo đẩy theo chủ đề, chúng tôi cũng có thể hủy đăng ký từ chủ đề.
Điều khiển:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @PostMapping("/subscribe") public void subscribeToTopic(@RequestBody SubscriptionRequestDto subscriptionRequestDto) { notificationService.subscribeToTopic(subscriptionRequestDto); } @PostMapping("/unsubscribe") public void unsubscribeFromTopic(SubscriptionRequestDto subscriptionRequestDto) { notificationService.unsubscribeFromTopic(subscriptionRequestDto); } @PostMapping("/topic") public String sendPnsToTopic(@RequestBody NotificationRequestDto notificationRequestDto) { return notificationService.sendPnsToTopic(notificationRequestDto); } |
ĐẾN:
1 2 3 4 5 6 7 8 | @Data public class NotificationRequestDto { private String target; private String title; private String body; } |
Dịch vụ:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | public void subscribeToTopic(SubscriptionRequestDto subscriptionRequestDto) { try { FirebaseMessaging.getInstance(firebaseApp).subscribeToTopic(subscriptionRequestDto.getTokens(), subscriptionRequestDto.getTopicName()); } catch (FirebaseMessagingException e) { log.error("Firebase subscribe to topic fail", e); } } public void unsubscribeFromTopic(SubscriptionRequestDto subscriptionRequestDto) { try { FirebaseMessaging.getInstance(firebaseApp).unsubscribeFromTopic(subscriptionRequestDto.getTokens(), subscriptionRequestDto.getTopicName()); } catch (FirebaseMessagingException e) { log.error("Firebase unsubscribe from topic fail", e); } } public String sendPnsToTopic(NotificationRequestDto notificationRequestDto) { Message message = Message.builder() .setTopic(notificationRequestDto.getTarget()) .setNotification(new Notification(notificationRequestDto.getTitle(), notificationRequestDto.getBody())) .putData("content", notificationRequestDto.getTitle()) .putData("body", notificationRequestDto.getBody()) .build(); String response = null; try { FirebaseMessaging.getInstance().send(message); } catch (FirebaseMessagingException e) { log.error("Fail to send firebase notification", e); } return response; } |
Tóm lược
Ở đây trong bài đăng này, chúng tôi mệt mỏi để bao gồm tích hợp Firebase Cloud Messaging bằng Spring Boot. Mã nguồn có sẵn trong GitHub .