英文:
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 = "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]
}
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
插件。
可以通过以下步骤实现:
- 删除现有的
cordova-plugin-fcm-with-dependency-updated
插件 - 移除 Android 平台
- 安装已解决问题的插件的分支版本
- 重新构建 Android 平台
英文:
Solution of this issue is to update the cordova-plugin-fcm-with-dependency-updated
plugin.
This can be achieved by
- Deleting the existing
cordova-plugin-fcm-with-dependency-updated
plugin - Removing the android platform
- Installing the forked version of the plugin where the issue has been resolved
- Rebuilding the android platform
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论