아래의 글을 참조했습니다.
https://sjevie.tistory.com/entry/TIL%EA%B0%9C%EB%85%90-Android-Notification-PendingIntent
아래와 같이 pendingintent를 작성할 때 putExtra로 값을 같이 전달하려고 할 경우
val intent = Intent(this@FCMService, MainActivity::class.java).apply {
putExtra("fromFCMRoute", WATCH_TOGETHER_MENU.RECEIVE_LIST.route)
}
val pendingIntent = PendingIntent.getActivity(
this@FCMService,
0, // requestCode가 고정값이면 나중에 putExtra 값이 null로 나오니 주의...
intent,
PendingIntent.FLAG_IMMUTABLE
)
getActivity의 두 번째 인자가 requestCode인데, 딱히 생각 없이 0으로 값을 고정했다가 계속 나중에 extra값을 받는 곳에서 null이 전달이 되더라...
찾아보니 notification마다 값을 다르게 전달해야 할 경우 requestCode를 전달할 값마다 다르게 설정해줘야 한다고 한다.
내가 작업하는 프로젝트의 경우 싱글액티비티에 컴포즈로 작업을 하고 있어서 intent로 activity를 다르게 실행하는게 아니기 때문에 별도로 extra값을 전달해서 그걸로 추가적인 동작을 해야하는 상황이었다.
이때 뭣도 모르고 requestCode값을 0으로만 넣고 실행하니 계속 extra값이 null로 나와서 당황했는데 LocalDate.now와 같이 변동하는 값으로 대체해주니 extra값이 잘 받아와졌다.
val intent = Intent(this@FCMService, MainActivity::class.java).apply {
putExtra("fromFCMRoute", WATCH_TOGETHER_MENU.RECEIVE_LIST.route)
}
val pendingIntent = PendingIntent.getActivity(
this@FCMService,
LocalTime.now().hashCode(), // requestCode가 고정값이면 나중에 putExtra 값이 null로 나오니 주의...
intent,
PendingIntent.FLAG_IMMUTABLE
)
앞으로 pendingIntent를 사용할 때 requestCode 값을 주의해서 넣어야 겠다.