Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE for FireBaseMessaging

huangapple go评论70阅读模式
英文:

Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE for FireBaseMessaging

问题

在尝试将我的移动应用程序(Ionic框架)的最新构建上传到Play商店时,我被迫将targetSDKVersion从30更新到31,这导致了与PendingIntent相关的许多问题。其中大部分我已经通过使用 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE 来替换 PendingIntent.FLAG_UPDATE_CURRENT or 0 来解决。

但是,有一个特定的Pending Intent错误,我怀疑与MyFirebaseMessagingService.java文件有关,但它只是被声明而没有实际使用。在这种情况下,我该如何解决这个问题?

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)

希望这些信息对解决问题有所帮助。

英文:

In my attempt to upload the latest build of my mobile application (Ionic framework) to playstore, I am forced to updated the targetSDKVersion from 30 to 31 which results in plenty of issues with PendingIntent. Most of which I have manage to resolve by utilizing PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
to replace PendingIntent.FLAG_UPDATE_CURRENT or 0 when PendingIntent is being used.

However, there is this particular error with the Pending Intent which I suspect is related to the MyFirebaseMessagingService.java file but it is only being declared and not actually used. How do I go about solving the issue in this case?

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 = &quot;FCMPlugin&quot;;
@Override
public void onNewToken(String token) {
super.onNewToken(token);
Log.d(TAG, &quot;New token: &quot; + 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, &quot;==&gt; MyFirebaseMessagingService onMessageReceived&quot;);
if(remoteMessage.getNotification() != null){
Log.d(TAG, &quot;\tNotification Title: &quot; + remoteMessage.getNotification().getTitle());
Log.d(TAG, &quot;\tNotification Message: &quot; + remoteMessage.getNotification().getBody());
}
Map&lt;String, Object&gt; data = new HashMap&lt;String, Object&gt;();
data.put(&quot;wasTapped&quot;, false);
if(remoteMessage.getNotification() != null){
data.put(&quot;title&quot;, remoteMessage.getNotification().getTitle());
data.put(&quot;body&quot;, remoteMessage.getNotification().getBody());
}
for (String key : remoteMessage.getData().keySet()) {
Object value = remoteMessage.getData().get(key);
Log.d(TAG, &quot;\tKey: &quot; + key + &quot; Value: &quot; + value);
data.put(key, value);
}
Log.d(TAG, &quot;\tNotification Data: &quot; + data.toString());
FCMPlugin.sendPushPayload(data);
}
// [END receive_message]
}

Error 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

得分: 0

解决此问题的方法是更新 cordova-plugin-fcm-with-dependency-updated 插件。

可以通过以下步骤实现:

  1. 删除现有的 cordova-plugin-fcm-with-dependency-updated 插件
  2. 移除 Android 平台
  3. 安装已解决问题的插件的分支版本
  4. 重新构建 Android 平台
英文:

Solution of this issue is to update the cordova-plugin-fcm-with-dependency-updated plugin.

This can be achieved by

  1. Deleting the existing cordova-plugin-fcm-with-dependency-updated plugin
  2. Removing the android platform
  3. Installing the forked version of the plugin where the issue has been resolved
  4. Rebuilding the android platform

https://github.com/andrehtissot/cordova-plugin-fcm-with-dependecy-updated/issues/293#issuecomment-1377487961

huangapple
  • 本文由 发表于 2023年5月18日 10:59:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定