sitelink1 | |
---|---|
sitelink2 | |
sitelink3 |
Admin SDK Reference의 java 라이브러리를 이용하여 notification을 fcm에 전송하는 예제를 알려드리겠습니다.
Admin SDK Reference는 Firebase Admin SDK를 사용하여 Firebase 서비스와 통신할 수 있는 API를 제공하는 문서입니다.
Firebase Admin SDK는 Firebase 앱을 위한 백엔드 서비스를 구축할 수 있게 해주는 라이브러리입니다.
Firebase Admin SDK는 다양한 프로그래밍 언어를 지원하는데, 여기서는 java 라이브러리를 사용하는 방법을 설명하겠습니다.
Firebase Admin SDK의 java 라이브러리를 사용하려면, 다음과 같은 절차를 따라야 합니다.
- Firebase 프로젝트를 생성하고, 자바 앱과 서버를 등록합니다. Firebase 콘솔에서 google-services.json 파일을 다운로드하고, 서버 프로젝트에 추가합니다. ¹
- 서버 프로젝트의 build.gradle 파일에 Firebase Admin SDK의 java 라이브러리를 의존성으로 추가합니다. ²
- 서버 프로젝트에서 FirebaseApp 클래스를 사용하여 Firebase 앱을 초기화합니다. ³
- 서버 프로젝트에서 FirebaseMessaging 클래스를 사용하여 FCM 서버 API를 호출하여 notification을 발송하는 로직을 구현합니다. ⁴
다음은 Admin SDK Reference의 java 라이브러리를 이용하여 notification을 fcm에 전송하는 예제 코드입니다.
[java]
// 서버 프로젝트의 build.gradle 파일
dependencies {
// Firebase Admin SDK의 java 라이브러리
implementation 'com.google.firebase:firebase-admin:8.0.1'
}
// 서버 프로젝트의 메인 클래스
public class MyServer {
// Firebase 앱을 초기화하는 메서드
public static void initializeFirebaseApp() {
try {
// google-services.json 파일의 경로를 지정
FileInputStream serviceAccount = new FileInputStream("path/to/google-services.json");
// Firebase 옵션을 설정
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build();
// Firebase 앱을 초기화
FirebaseApp.initializeApp(options);
} catch (IOException e) {
// 예외 처리
e.printStackTrace();
}
}
// notification을 발송하는 메서드
public static void sendNotification(String token, String title, String body) {
try {
// notification 객체를 생성
Notification notification = Notification.builder()
.setTitle(title)
.setBody(body)
.build();
// 메시지 객체를 생성
Message message = Message.builder()
.setNotification(notification)
.setToken(token)
.build();
// FirebaseMessaging 객체를 가져옴
FirebaseMessaging firebaseMessaging = FirebaseMessaging.getInstance();
// 메시지를 발송하고 결과를 받음
String response = firebaseMessaging.send(message);
// 결과를 콘솔에 출력
System.out.println("Successfully sent message: " + response);
} catch (FirebaseMessagingException e) {
// 예외 처리
e.printStackTrace();
}
}
// 메인 메서드
public static void main(String[] args) {
// Firebase 앱을 초기화
initializeFirebaseApp();
// notification을 발송
sendNotification("fh1Ego6mhk0:APA91bF...", "FCM Message", "This is an FCM Message");
}
}
Admin SDK Reference의 java 라이브러리를 이용하여 notification을 fcm에 전송하는 예제를 알려드렸습니다.
Firebase Admin SDK의 java 라이브러리를 사용하면, 자바 앱에서 Firebase 서비스와 통신할 수 있습니다.
Admin SDK Reference의 java 라이브러리에 대한 자세한 내용은 다음 웹 사이트를 참고하세요.
- [Admin SDK Reference | Firebase](^1^): Firebase Admin SDK의 API 문서를 제공합니다.
- [Firebase Admin SDK를 사용하여 서버 환경 설정 | Firebase](^2^): Firebase Admin SDK의 java 라이브러리를 사용하는 방법을 제공합니다.
- [Firebase Admin SDK를 사용하여 FCM 알림 보내기 | Firebase](^3^): Firebase Admin SDK의 java 라이브러리를 사용하여 notification을 발송하는 방법을 제공합니다.
출처: Bing과의 대화, 2023. 11. 27.
(1) Firebase를 이용한 Push Notification - Medium. https://medium.com/@vdongbin/firebase%EB%A5%BC-%EC%9D%B4%EC%9A%A9%ED%95%9C-push-notification-5c8a83932472.
(2) FCM push notification을 위한 앱서버 구현하기 1 - 기본개념 및 core .... https://www.irgroup.org/posts/fcm-app-server-1/.
(3) [Spring Boot] FCM을 통해 Push 알림 보내기 1 - 벨로그. https://velog.io/@leesomyoung/Spring-Boot-FCM을-통해-Push-알림-보내기-1.
(4) [Capstone] FCM을 통해 Push 알림 서버 구축하기 - 벨로그. https://velog.io/@daydream/Spring-FCM을-통해-Push-알림-서버-구축.
(5) Firebase를 이용한 Push Notification - Medium. https://medium.com/@vdongbin/firebase%EB%A5%BC-%EC%9D%B4%EC%9A%A9%ED%95%9C-push-notification-5c8a83932472.
(6) FCM push notification을 위한 앱서버 구현하기 1 - 기본개념 및 core .... https://www.irgroup.org/posts/fcm-app-server-1/.
(7) [Spring Boot] FCM을 통해 Push 알림 보내기 1 - 벨로그. https://velog.io/@leesomyoung/Spring-Boot-FCM을-통해-Push-알림-보내기-1.
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
13 | FirebaseMessagingException: Requested entity was not found. | 황제낙엽 | 2024.01.12 | 1511 |
» | (Copilot) Admin SDK Reference의 java 라이브러리를 이용하여 notification을 fcm에 전송하는 java 예제 | 황제낙엽 | 2023.11.28 | 205 |
11 | firebase.messaging().getToken() 함수와 pushManager.subscribe() 함수의 관계 | 황제낙엽 | 2023.11.26 | 275 |
10 | [FCM] FCM 으로 알림 전송 테스트 (spring boot + android + fcm rest) | 황제낙엽 | 2023.11.24 | 178 |
9 |
[FCM] Firebase Console 에서 메세지 보내기
![]() | 황제낙엽 | 2023.11.24 | 156 |
8 |
(OMNIBUSCODE/FCM/WEB/JAVA) web push notification (web browser) 샘플
![]() | 황제낙엽 | 2023.11.23 | 146 |
7 | (Copilot) Notification Server 의 종류 | 황제낙엽 | 2023.11.23 | 141 |
6 | (OMNIBUSCODE/FCM/WEB/JAVA) web push notification (web browser) 구현 절차 [1] | 황제낙엽 | 2023.11.20 | 190 |
5 | 푸쉬 알림 개발 관련 레퍼런스 | 황제낙엽 | 2023.11.09 | 174 |
4 | [bard] web-push와 fcm 의 차이 | 황제낙엽 | 2023.11.08 | 224 |
3 | PWA 관련 링크 모음 | 황제낙엽 | 2023.11.06 | 165 |
2 | FCM (firebase cloud message) 사용하기 | 황제낙엽 | 2021.02.09 | 189 |
1 | FCM 과 GCM 차이 | 황제낙엽 | 2019.02.19 | 311 |