FCM

Error getting access token for service account 오류 해결

쿠카이든 2023. 10. 12. 18:45
728x90

원인 : 스프링 부트 스케쥴러와 트랙잭션을 따로 동작시키면 Error getting access token for service account 오류가 발생

@Service
@Transactional
class NotiService {
    private val pushInfoJpaRepo: PushInfoJpaRepo,

) {
    val log = KotlinLogging.logger {}

    fun sendPush(repeatedPushResults: List<PushReservedInfoDTO>?) {
        val pushReservedInfo = PushReservedInfo()
        if (repeatedPushResults != null) {
            for (repeatedPushResult in repeatedPushResults) {
                if (userNtcnSetupInfoJpaRepo.findByUserInfoSeqAndNtcnType(
                        userInfoJpaRepo.findByUserLoginId(repeatedPushResult.userLoginId.toString())?.userInfoSeq,
                        "쇼핑이벤트알림"
                    ) != null
                ) {
                    try {
                        if (!repeatedPushResult.userLoginId.isNullOrEmpty()) {
                            val pushRefreshTkn =
                                userInfoJpaRepo.findByUserLoginId(repeatedPushResult.userLoginId!!)?.pushRefreshTkn
                            firebaseCloudMessageService.sendMessageTo(
                                pushRefreshTkn,
                                repeatedPushResult.pushUpendNm,
                                repeatedPushResult.pushText,
                                null
                            )

                            if (repeatedPushResult.pushSeq != null) {
                                val pushSndHistInfoEntity =
                                    pushSndHistInfoJpaRepo.findByPushInfoPushSeq(repeatedPushResult.pushSeq!!)
                                pushSndHistInfoEntity?.apply {
                                    pushInfo = pushInfoJpaRepo.getReferenceById(repeatedPushResult.pushSeq!!)
                                    pushSeq = pushSndHistInfoEntity.pushSeq
                                    sndTargtCnt = repeatedPushResults?.size ?: 0
                                    if (pushInfoJpaRepo.findById(repeatedPushResult.pushSeq!!)
                                            .get().osNm?.equals("ANDROID") == true
                                    ) {
                                        aosSndSucesCnt = pushSndHistInfoEntity?.aosSndSucesCnt?.plus(1)
                                    } else {
                                        iosSndSucesCnt = pushSndHistInfoEntity?.iosSndSucesCnt?.plus(1)
                                    }
                                    aosSndFailCnt = pushSndHistInfoEntity?.aosSndFailCnt
                                    iosSndFailCnt = pushSndHistInfoEntity?.iosSndFailCnt
                                    sndDtt = pushSndHistInfoEntity?.sndDtt ?: LocalDateTime.now()
                                    sndCmpltDtt = LocalDateTime.now()
                                }
                                if (pushSndHistInfoEntity != null) {
                                    pushSndHistInfoJpaRepo.save(pushSndHistInfoEntity)
                                }
...
  • 기존에는 발송하는 서비스 부분을 위와 같이 다른 트랜잭션을 타도록 분리하였다.
  • 하지만 스프링 부트 스케쥴러와 같이 사용하자 Error getting access token for service account  오류가 발생하였다.

 

해결 : 스케쥴러와 서비스를 같은 트랙잭션 안에서 동작하도록 함, 즉 아래와 같은 코드로 수정하여 푸시알림이  스케쥴러에 영향을 받지 않고 잘 동작되도록 할 수 있었다.

@Component
class PushScheduler(
    private val pushInfoJpaRepo: PushInfoJpaRepo,
    ...
) {
    val log = KotlinLogging.logger {}

    @Scheduled(cron = "0 30 8-20 * * *")   //30분 단위 스케쥴링 실행
    fun sendDayReservedPushBy30Minute() {
        log.debug { "예약 푸시 조회 시간 -> ${LocalDateTime.now()}" }

        val reservedPushResults = pushReservedInfoQdslRepo.findPushReservedInfosBy30Minutes(
            LocalDateTime.now().minusMinutes(15),
            LocalDateTime.now().plusMinutes(15)
        )
        sendPush(reservedPushResults)
    }

    fun sendPush(repeatedPushResults: List<PushReservedInfoDTO>?) {
        val pushReservedInfo = PushReservedInfo()
        if (repeatedPushResults != null) {
            for (repeatedPushResult in repeatedPushResults) {
                if (userNtcnSetupInfoJpaRepo.findByUserInfoSeqAndNtcnType(
                        userInfoJpaRepo.findByUserLoginId(repeatedPushResult.userLoginId.toString())?.userInfoSeq,
                        "쇼핑이벤트알림"
                    ) != null
                ) {
                    try {
                        if (!repeatedPushResult.userLoginId.isNullOrEmpty()) {
                            val pushRefreshTkn =
                                userInfoJpaRepo.findByUserLoginId(repeatedPushResult.userLoginId!!)?.pushRefreshTkn
                            firebaseCloudMessageService.sendMessageTo(
                                pushRefreshTkn,
                                repeatedPushResult.pushUpendNm,
                                repeatedPushResult.pushText,
                                null
                            )
                            if (repeatedPushResult.pushSeq != null) {
                                val pushSndHistInfoEntity =
                                    pushSndHistInfoJpaRepo.findByPushInfoPushSeq(repeatedPushResult.pushSeq!!)
                                pushSndHistInfoEntity?.apply {
                                    pushSndHistSeq = pushSndHistInfoEntity.pushSndHistSeq
                                    pushInfo = pushInfoJpaRepo.getReferenceById(repeatedPushResult.pushSeq!!)
                                    pushSeq = pushSndHistInfoEntity.pushSeq
                                    sndTargtCnt = repeatedPushResults?.size ?: 0
                                    if (pushInfoJpaRepo.findById(repeatedPushResult.pushSeq!!)
                                            .get().osNm?.equals("ANDROID") == true
                                    ) {
                                        aosSndSucesCnt = pushSndHistInfoEntity?.aosSndSucesCnt?.plus(1)
                                    } else {
                                        iosSndSucesCnt = pushSndHistInfoEntity?.iosSndSucesCnt?.plus(1)
                                    }
                                    aosSndFailCnt = pushSndHistInfoEntity?.aosSndFailCnt
                                    iosSndFailCnt = pushSndHistInfoEntity?.iosSndFailCnt
                                    sndDtt = pushSndHistInfoEntity?.sndDtt ?: LocalDateTime.now()
                                    sndCmpltDtt = LocalDateTime.now()
                                }
                                ...

 

728x90

'FCM' 카테고리의 다른 글

(FCM)서버-IOS 연동 시 발생한 예외 사항 대처 방법  (0) 2023.08.04