FCM으로 푸쉬 알림을 보내는 부분에 대한 간략한 기록
앱이 foreground 상태라면 onMessageReceived 함수를 호출하여 notification을 호출할 수 있고,
앱이 background, killed 상태라면 onMessageReceived를 호출하지 않고, 시스템 트레이를 통하여 notification을 호출한다.
라고 이해하였으나 생각보다 페이로드(데이터의 형식)에 따라 작동 방식이 조금씩 달라진다.
1. notification만 담는 경우
{
"message":{
"notification":{
"title" : "title"
"body" : "body",
}
}
일반적인 notification만 담아서 보내는 경우 background, killed 상태라면 시스템 트레이를 통해,
foreground 상태라면 onMessageReceived 함수를 통해 알람을 호출할 수 있다.
2. notification + data를 담는 경우
{
"message":{
"notification":{
"title" : "title"
"body" : "body",
},
"data":{
"url" : "url"
}
}
notification + data 를 보낼경우,
foreground 상태에서는 data가 성공적으로 보내지지만, 그 외의 경우에는 data는 실제로 전송되지 못한다.
(background 상태에서는 알람을 클릭해야만 data가 전송된다.)
또한 foreground 상태에서는 notification를 무시한다.
즉 background, killed 상태에서는 data를 전송받지 못하기때문에, data에 게시판 id등을 담아서, 알림을 클릭하면 바로 해당 게시글로 이동하는것등의 구현이 어렵다.
3. data만 담는 경우
{
"message":{
"data":{
"url" : "url"
}
}
notification 없이 data만 담을 경우, background, killed 상태에서도 onMessageReceived 함수를 호출한다.
sendNotification 함수를 호출해서 담은 data를 기반으로 푸쉬 알림을 보내는것도 여전히 가능하다.
시스템트레이를 이용한 notification은 푸쉬알림을 여러개 쌓을 수 있지만, data만을 넘길경우 푸쉬알림이 항상 최근의 것으로 교체되는 차이가 있다. (옵션을 통해 notification도 알림이 쌓이는것을 막을 수 있는것 같다)
Reference: firebase.google.com/docs/cloud-messaging/concept-options?hl=ko
'Android > Tips' 카테고리의 다른 글
Recyclerview adapter 재활용하기 (0) | 2021.11.15 |
---|---|
TabLayout (0) | 2021.11.15 |
Retrofit2 Multiple BaseUrl (Java) (0) | 2021.03.25 |
ViewPager2 오버랩 화면전환 (pageTransformer) (0) | 2021.01.05 |
Retrofit2 MultiPart로 이미지 파일 전송하기 (Kotlin) (0) | 2020.12.02 |