15. [안드로이드/java] FCM Push 알림 클릭 안되는 문제
알림이 오긴 하나 클릭해도 반응하지 않습니다.
이 부분을 수정하기 위해서 Notification에 Intent를 추가해야 합니다.
이번 게시글은 아래 게시글 내용과 연동됩니다.
https://like-a-drizzle.tistory.com/248
14. [안드로이드/java] FCM Push 알림 (포그라운드, 백그라운드 처리)
이전에 FCM Push 알림 기능에 대해서 포스팅한 적이 있는데 알림 기능에 문제가 있었습니다. 1. 앱이 종료된 상태 2. 앱이 화면에 표시되어 있지 않은 상태 즉, 백그라운드 상태에서는 알림이 오는
like-a-drizzle.tistory.com
1. MyFirebaseMessagingService NOTIFICATION_ID 변수를 선언합니다.
private static final int NOTIFICATION_ID = 0;
2. MyFirebaseMessagingSerive.java에 Intent와 PendingIntent 변수를 생성합니다.
PendingIntent - Intent를 감싸는 Intent, Notification에서는 Intent를 직접적으로 사용하지 못하고 PendingIntent를 사용하여야 합니다.
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent notificationPendingIntend = PendingIntent.getActivity(this, NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
3. MyFirebaseMessagingService.java > getNotificationBuilder() 메서드 내에서 Builder에 Intent 적용
setContentIntent는 Builder(Notification 구현)에 Intent를 적용시키는 것이고,
setAutoCancel은 사용자가 Notification을 눌렀을 경우 Notification을 없앨 것인지 여부입니다.
4. 결과
postman으로 전송
{
"to":"기기_토큰값",
"priority" : "high",
"data" : {
"title" : "title",
"message" : "Hello, World!"
}
}
정상적으로 notification을 수신하고 클릭 시 앱이 실행됩니다.
Module : build.gralde 설정과 이전과 다르게 변경했습니다.
implementation platform('com.google.firebase:firebase-bom:26.2.0')
implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.firebase:firebase-analytics'