Android Studio :当应用程序最小化或关闭时,Firebase推送通知不工作

nue99wik  于 2023-02-05  发布在  Android
关注(0)|答案(2)|浏览(121)

在gradle文件中将compileSdkVersion从30更新到〉31后,Android手机在应用最小化或关闭时停止接收通知。
Gradle文件:

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'

清单文件:

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

错误信息:

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)

有人能提供一些建议吗?

r8uurelv

r8uurelv1#

尝试在gradle中添加此依赖关系:

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

此外,如TomInCode所述,将Firebase依赖项更新为最新版本。

carvr3hs

carvr3hs2#

您需要在PendingIntent中设置FLAG_IMMUTABLE或FLAG_MUTABLE,如下所示

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

相关问题