You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级TargetSDK31后FirebaseMessaging的PendingIntent异常求助

升级targetSDKVersion到31后Firebase Messaging的PendingIntent异常解决

我基于Ionic开发的移动应用,上传PlayStore时被迫将targetSDKVersion从30升级至31,这引发了大量PendingIntent相关问题。多数问题已通过使用PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE替换PendingIntent.FLAG_UPDATE_CURRENT或0的方式解决,但仍存在一个关联MyFirebaseMessagingService.java的PendingIntent异常,该服务仅被声明未实际使用,请问该如何解决此问题?


MyFirebaseMessagingService.java

package com.gae.scaffolder.plugin;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;
import java.util.Map;
import java.util.HashMap;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "FCMPlugin";

    @Override
    public void onNewToken(String token) {
        super.onNewToken(token);
        Log.d(TAG, "New token: " + token);
        FCMPlugin.sendTokenRefresh(token);
    }

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO(developer): Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
        Log.d(TAG, "==> MyFirebaseMessagingService onMessageReceived");
        
        if(remoteMessage.getNotification() != null){
            Log.d(TAG, "\tNotification Title: " + remoteMessage.getNotification().getTitle());
            Log.d(TAG, "\tNotification Message: " + remoteMessage.getNotification().getBody());
        }
        
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("wasTapped", false);
        
        if(remoteMessage.getNotification() != null){
            data.put("title", remoteMessage.getNotification().getTitle());
            data.put("body", remoteMessage.getNotification().getBody());
        }

        for (String key : remoteMessage.getData().keySet()) {
            Object value = remoteMessage.getData().get(key);
            Log.d(TAG, "\tKey: " + key + " Value: " + value);
            data.put(key, value);
        }
        
        Log.d(TAG, "\tNotification Data: " + data.toString());
        FCMPlugin.sendPushPayload(data);
    }
    // [END receive_message]
}

错误信息

FATAL EXCEPTION: Firebase-MyFirebaseMessagingService
Process: ----- , PID: 16071
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:408)
at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:491)
at android.app.PendingIntent.getActivity(PendingIntent.java:477)
at android.app.PendingIntent.getActivity(PendingIntent.java:441)
at com.google.firebase.messaging.zzb.zzf(Unknown Source:68)
at com.google.firebase.messaging.zzc.zzas(Unknown Source:34)
at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source:59)
at com.google.firebase.iid.zzb.run(Unknown Source: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(Unknown Source:6)
at java.lang.Thread.run(Thread.java:1012)

解决方案

1. 升级Firebase Messaging依赖版本

这个异常的根源是旧版本的Firebase Messaging库未适配Android 12(targetSDK31+)的PendingIntent规则,创建PendingIntent时没有指定FLAG_IMMUTABLE或FLAG_MUTABLE标记:

  • 确保com.google.firebase:firebase-messaging的版本升级到21.0.0及以上(该版本开始原生支持Android 12的PendingIntent要求)。
  • 如果你使用的是Ionic的FCM相关插件(如cordova-plugin-fcm-with-dependecy-updated),需确认插件依赖的Firebase库版本已更新,或者手动在Android项目的build.gradle文件中指定最新依赖版本。

2. 移除未使用的服务

既然MyFirebaseMessagingService仅被声明但未实际使用,直接清理相关配置即可:

  • 在AndroidManifest.xml中删除对应<service>声明(通常是包含android:name="com.gae.scaffolder.plugin.MyFirebaseMessagingService"的节点)。
  • 删除项目中的MyFirebaseMessagingService.java文件,彻底移除该服务的触发路径。

3. 验证FCM配置完整性

如果后续需要保留FCM功能,需确保所有与通知相关的PendingIntent创建逻辑都添加了正确的标记,但由于你当前并未使用该服务,前两种方案是最直接有效的。


内容的提问来源于stack exchange,提问作者Yeo Bryan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 18:05:35