안드로이드에 외부에서 알림을 보내고자 한다.
로직은 간단하다.
서버 -> firebase -> android
firebase와 android 초기 설정은 https://blog.naver.com/yeob07/221848594108
[android] 구글 로그인 연동 (firebase 사용)
firebase를 이용한 구글 로그인 연동1. fireabase 프로젝트 만들기https://firebase.google.com/?hl...
blog.naver.com
https://firebase.google.com/docs/cloud-messaging/android/client?hl=ko
Android에서 Firebase 클라우드 메시징 클라이언트 앱 설정
Firebase 클라우드 메시징 Android 클라이언트 앱을 만들려면 FirebaseMessaging API와 Gradle이 있는 Android 스튜디오 1.4 이상을 사용하세요. 이 페이지의 안내에서는 Android 프로젝트에 Firebase를 추가하는 단�
firebase.google.com
참고
매니패스트에 퍼미션과 서비스 추가
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application>
...
<activity>
...
</activity>
<service
android:name=".FireBaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".FirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
...
</application>
gradle 추가 (app)
implementation 'com.google.firebase:firebase-messaging:12.0.1'
java 코드는 https://yeolco.tistory.com/85 에서 참고하였음
안드로이드 FCM 푸시알림 예제
안드로이드 앱을 개발하다보면 푸시알림 기능이 필요한 부분이 존재합니다. 기존의 GCM(Google Cloud Messaging) 서비스가 대표적이었는데, 최근 구글에서 Firebase를 인수, FCM(Firebase Cloud Messaging) 서비..
yeolco.tistory.com
Jinyeob/MyFCM
Android - Push notification using firebase . Contribute to Jinyeob/MyFCM development by creating an account on GitHub.
github.com
FirebaseInstanceIDService.java
Jinyeob/MyFCM
Android - Push notification using firebase . Contribute to Jinyeob/MyFCM development by creating an account on GitHub.
github.com
테스트해보자
Send your first message 클릭
아무거나 입력 후 다음 클릭
검토 클릭.
이렇게 뜨고, 안드로이드 앱에서도 뜨면 성공!
빌드 후, logcat 에서 토큰을 검색한다.
토큰 복사
이제 서버 쪽에서 firebase로 보내보자.
먼저,
npm install fcm-node
명령어 실행하여 설치. (제 환경: Windows10 node.js)
serverKey 복사
firebase -> Settings -> 클라우드 메시징 탭에 있다.
js 파일 생성 (참고: https://haams704.tistory.com/17)
FCM 푸시 [서버편]
오늘은 Firebase를 활용하여 Notification , 즉 푸시알림을 보내는 방법에 대해서 설명드리고자 합니다. 우선 기본적으로 개념은 Firebase에서 프로젝트를 만들고 거기서 얻은 서버키를 가지고 fcm-node 패
haams704.tistory.com
var FCM = require('fcm-node');
var serverKey = ''; //서버키
var fcm = new FCM(serverKey);
var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera)
to: '', //앱에서 복사한 토큰
notification: {
title: '제목',
body: '내용'
},
data: { //you can send only notification or only data(or include both)
my_key: 'my value',
my_another_key: 'my another value'
}
};
fcm.send(message, function(err, response){
if (err) {
console.log("Something has gone wrong!");
} else {
console.log("Successfully sent with response: ", response);
}
});
serverKey에 서버키, to: 에 토큰을 입력한다.
저장 후, cmd에서 돌려보자.
이렇게 출력하고, 앱에서도 알림이 뜨면 성공!
'프로그래밍 > App 개발' 카테고리의 다른 글
[android] MPAndroidChart를 활용한 실시간 차트 그리기 (0) | 2020.08.06 |
---|---|
[android] MVVM의 편리함 (20.05.28 깨달은 점) (0) | 2020.08.06 |
[android] 데이터바인딩 (데이터결합 라이브러리) (0) | 2020.08.06 |
[android] foreground service 예제 (0) | 2020.08.06 |
[android] 내 위치 sms 전송하기 (카카오맵) (0) | 2020.08.06 |