숨참고 개발다이브

[안드로이드/오류] 푸시 FirebaseMessagingService IllegalArgumentException 오류 본문

개발/Android

[안드로이드/오류] 푸시 FirebaseMessagingService IllegalArgumentException 오류

사라 Sarah 2022. 6. 6. 10:37
728x90
AndroidRuntime: FATAL EXCEPTION: MQTT Rec: com.app.mobile-2e24ccbde048f2e91635651784
    Process: com.app.mobile, PID: 17031
    java.lang.IllegalArgumentException: com.app.mobile: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
    Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
        at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
        at org.eclipse.paho.android.service.AlarmPingSender.start(AlarmPingSender.java:76)
        at org.eclipse.paho.client.mqttv3.internal.ClientState.connected(ClientState.java:1150)
        at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:987)
        at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:118)
        at java.lang.Thread.run(Thread.java:920)

 

이런 오류가 발생했다.

이유는 FirebaseMessagingService에서 pendingIntent를 설정할 때 FLAG_ONE_SHOT를 사용했기 때문이다.

기존에 사용하던 코드를 그대로 가져와 사용했더니 타겟 SDK가 32가 되면서 이런 오류가 발생하게 되었다.

 

 

해결방법

 

build.gradle(app) 에 다음을 추가하면 된다.

dependencies {
  // Java
  implementation 'androidx.work:work-runtime:2.7.1' 

  // Kotlin
  implementation 'androidx.work:work-runtime-ktx:2.7.1'
}

 

해당 코드를 추가해도 오류가 발생한다면 pendingIntent에 Flag를 수정해주어야 한다.

FLAG_IMMUTABLE 혹은 FLAG_MUTABLE로 교체해주면 된다.

 

 

 

 

PendingIntent의 Flag 속성은 다음과 같다.

FLAG_CANCEL_CURRENT : 이전에 생성한 PendingIntent를 취소하고 새로 생성한다.
FLAG_IMMUTABLE : PendingIntent는 변경 불가능하도록 설정한다.
FLAG_MUTABLE : PendingIntent가 변경 가능하도록 설정한다.
FLAG_NO_CREATE : PendingIntent가 존재한다면 재사용을, 존재하지 않으면 null을 리턴한다.
FLAG_ONE_SHOT : 일회성 PendingIntent이다.
FLAG_UPDATE_CURRENT : PendingIntent가 존재하면 기존 PendingIntent를 유지하고 Extra Data만 변경해준다.

 

 

공식 문서에서도 다음과 같이 속성 확인이 가능하다.

 

 

300x250
Comments