Home > Net >  Android Studio: Firebase Push Notifications not working when app is minimized or closed
Android Studio: Firebase Push Notifications not working when app is minimized or closed

Time:02-04

After updating compileSdkVersion from 30->31 in gradle file, android phones have stopped receiving notifications when the app is minimized or closed.

Gradle File:

implementation 'com.google.firebase:firebase-analytics:17.2.2'
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-firestore:21.4.0'
implementation 'com.google.firebase:firebase-messaging:20.1.0'
implementation 'com.google.firebase:firebase-analytics:17.2.2'
implementation 'com.google.android.gms:play-services-location:18.0.0'

Manifest File:

<service android:name=".service.ABCMessaging" android:exported="true">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

Error message:

E/AndroidRuntime: FATAL EXCEPTION: Firebase-AbcMessaging
    Process: <>, PID: 29537
    java.lang.IllegalArgumentException: <>: 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:377)
        at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:460)
        at android.app.PendingIntent.getActivity(PendingIntent.java:446)
        at android.app.PendingIntent.getActivity(PendingIntent.java:410)
        at com.google.firebase.messaging.zzb.zza(com.google.firebase:firebase-messaging@@20.1.0:59)
        at com.google.firebase.messaging.zzd.zza(com.google.firebase:firebase-messaging@@20.1.0:33)
        at com.google.firebase.messaging.FirebaseMessagingService.zzc(com.google.firebase:firebase-messaging@@20.1.0:69)
        at com.google.firebase.messaging.zze.run(com.google.firebase:firebase-messaging@@20.1.0:2)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
        at com.google.android.gms.common.util.concurrent.zza.run(com.google.android.gms:play-services-basement@@18.0.0:2)
        at java.lang.Thread.run(Thread.java:1012)

Can someone please provide any pointers?

CodePudding user response:

You need to set FLAG_IMMUTABLE or FLAG_MUTABLE in your PendingIntent like below

    PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, notificationIntent, FLAG_IMMUTABLE);

CodePudding user response:

Try adding this dependency in the gradle:

implementation 'androidx.work:work-runtime:2.7.0-alpha05'

Also, as TomInCode mentioned, update the Firebase dependencies to the latest version.

  • Related