如何修复应用程序在接收基于 Flutter 构建的 FCM 推送通知时崩溃
How to fix app crash on recieving FCM Push Notification built on Flutter
我在 Flutter 上构建了一个应用程序,并试图通过 FCM 发送推送通知。早些时候它可以工作,但在更改 Firebase 消息传递的版本后,只要我发送推送通知,应用程序就会崩溃。
我没有对代码本身进行任何更改。我调查了 GitHub 问题,但无法解决。
这是我在 main.dart 文件中使用的代码。
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
void initState() {
super.initState();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print('on message $message');
},
onResume: (Map<String, dynamic> message) {
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message) {
print('on launch $message');
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.getToken().then((token){
print(token);
});
我的 firebase_messaging 依赖关系
firebase_messaging: ^1.0.2
我也在app/build中更改了google-services的版本。gradle
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.google.gms:google-services:3.2.0'
}
这是为通知添加到 AndroidManifest.xml 文件的部分
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
设备日志
03-24 19:14:25.044 2630 2663 I ActivityManager: Start proc 17709:com.example.notif_app/u0a206 for broadcast com.example.notif_app/com.google.firebase.iid.FirebaseInstanceIdReceiver
03-24 19:14:25.047 17709 17709 I art : Late-enabling -Xcheck:jni
03-24 19:14:25.181 17709 17709 D FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
03-24 19:14:25.182 17709 17709 D FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
03-24 19:14:25.182 17709 17709 I FirebaseInitProvider: FirebaseApp initialization successful
03-24 19:14:25.199 17709 17730 I ResourceExtractor: Found extracted resources res_timestamp-1-1553431828587
03-24 19:14:25.206 17709 17727 I FA : App measurement is starting up, version: 15300
03-24 19:14:25.206 17709 17727 I FA : To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
03-24 19:14:25.207 17709 17727 I FA : To enable faster debug mode event logging run:
03-24 19:14:25.207 17709 17727 I FA : adb shell setprop debug.firebase.analytics.app com.example.notif_app
03-24 19:14:25.220 17709 17709 D AndroidRuntime: Shutting down VM
03-24 19:14:25.221 17709 17709 E AndroidRuntime: FATAL EXCEPTION: main
03-24 19:14:25.221 17709 17709 E AndroidRuntime: Process: com.example.notif_app, PID: 17709
03-24 19:14:25.221 17709 17709 E AndroidRuntime: java.lang.NoSuchMethodError: No static method zzad()Lcom/google/firebase/iid/zzan; in class Lcom/google/firebase/iid/zzan; or its super classes (declaration of 'com.google.firebase.iid.zzan' appears in /data/app/com.example.notif_app-1/base.apk:classes2.dex)
03-24 19:14:25.221 17709 17709 E AndroidRuntime: at com.google.firebase.messaging.FirebaseMessagingService.zzb(Unknown Source)
03-24 19:14:25.221 17709 17709 E AndroidRuntime: at com.google.firebase.iid.zzb.onStartCommand(Unknown Source)
03-24 19:14:25.221 17709 17709 E AndroidRuntime: at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3318)
03-24 19:14:25.221 17709 17709 E AndroidRuntime: at android.app.ActivityThread.-wrap21(ActivityThread.java)
谁能帮我解决这个问题?提前致谢。
打开 android 项目。
去firebase_messagingbuild.gradle
在依赖项中将 api 'com.google.firebase:firebase-messaging:18.0.0'
更改为
api 'com.google.firebase:firebase-messaging:17.3.3'
确保您使用的是 pubspec.yaml
中所有云依赖项的最新版本
这个解决方案对我有用
只是 运行 flutter clean
和 运行 您的应用程序。
就我而言,我遵循了插件说明https://pub.dev/packages/firebase_messaging
但是,我的 Android 应用还是崩溃了。
原因在文件中android/app/src/main/kotlin/com/example/app/MainActivity.kt
当我把它改成
package YOUR.APP.PACKAGE
import io.flutter.embedding.android.FlutterActivity
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
class Application : FlutterApplication(), PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
override fun registerWith(registry: PluginRegistry?) {
io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}
}
崩溃消失了。
同样为了以防万一,监听即将到来的推送的代码,重要的是要有 onBackgroundMessage
的处理程序
static Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async { print(message); return Future<void>.value(); }
firebaseMessaging.configure(
// when app is open
onMessage: (Map<String, dynamic> message) async {
try {
Get.defaultDialog(
title: message['notification']['title'] ?? 'New reaction to comment',
middleText: message['notification']['body'] ?? 'Click to react',
textCancel: 'Cancel',
textConfirm: 'See',
onCancel: () {},
onConfirm: () => PushServices.managePushesLinkToProject(message));
} catch (e) {
print('$e');
}
},
onLaunch: (Map<String, dynamic> message) async {
PushServices.managePushesLinkToProject(message);
},
onResume: (Map<String, dynamic> message) async {
PushServices.managePushesLinkToProject(message);
},
onBackgroundMessage: myBackgroundMessageHandler,
);
这里找到了关键的解决方案https://github.com/FirebaseExtended/flutterfire/issues/2311
我在 Flutter 上构建了一个应用程序,并试图通过 FCM 发送推送通知。早些时候它可以工作,但在更改 Firebase 消息传递的版本后,只要我发送推送通知,应用程序就会崩溃。
我没有对代码本身进行任何更改。我调查了 GitHub 问题,但无法解决。
这是我在 main.dart 文件中使用的代码。
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
void initState() {
super.initState();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print('on message $message');
},
onResume: (Map<String, dynamic> message) {
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message) {
print('on launch $message');
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.getToken().then((token){
print(token);
});
我的 firebase_messaging 依赖关系
firebase_messaging: ^1.0.2
我也在app/build中更改了google-services的版本。gradle
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.google.gms:google-services:3.2.0'
}
这是为通知添加到 AndroidManifest.xml 文件的部分
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
设备日志
03-24 19:14:25.044 2630 2663 I ActivityManager: Start proc 17709:com.example.notif_app/u0a206 for broadcast com.example.notif_app/com.google.firebase.iid.FirebaseInstanceIdReceiver
03-24 19:14:25.047 17709 17709 I art : Late-enabling -Xcheck:jni
03-24 19:14:25.181 17709 17709 D FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
03-24 19:14:25.182 17709 17709 D FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
03-24 19:14:25.182 17709 17709 I FirebaseInitProvider: FirebaseApp initialization successful
03-24 19:14:25.199 17709 17730 I ResourceExtractor: Found extracted resources res_timestamp-1-1553431828587
03-24 19:14:25.206 17709 17727 I FA : App measurement is starting up, version: 15300
03-24 19:14:25.206 17709 17727 I FA : To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
03-24 19:14:25.207 17709 17727 I FA : To enable faster debug mode event logging run:
03-24 19:14:25.207 17709 17727 I FA : adb shell setprop debug.firebase.analytics.app com.example.notif_app
03-24 19:14:25.220 17709 17709 D AndroidRuntime: Shutting down VM
03-24 19:14:25.221 17709 17709 E AndroidRuntime: FATAL EXCEPTION: main
03-24 19:14:25.221 17709 17709 E AndroidRuntime: Process: com.example.notif_app, PID: 17709
03-24 19:14:25.221 17709 17709 E AndroidRuntime: java.lang.NoSuchMethodError: No static method zzad()Lcom/google/firebase/iid/zzan; in class Lcom/google/firebase/iid/zzan; or its super classes (declaration of 'com.google.firebase.iid.zzan' appears in /data/app/com.example.notif_app-1/base.apk:classes2.dex)
03-24 19:14:25.221 17709 17709 E AndroidRuntime: at com.google.firebase.messaging.FirebaseMessagingService.zzb(Unknown Source)
03-24 19:14:25.221 17709 17709 E AndroidRuntime: at com.google.firebase.iid.zzb.onStartCommand(Unknown Source)
03-24 19:14:25.221 17709 17709 E AndroidRuntime: at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3318)
03-24 19:14:25.221 17709 17709 E AndroidRuntime: at android.app.ActivityThread.-wrap21(ActivityThread.java)
谁能帮我解决这个问题?提前致谢。
打开 android 项目。
去firebase_messagingbuild.gradle
在依赖项中将 api 'com.google.firebase:firebase-messaging:18.0.0'
更改为
api 'com.google.firebase:firebase-messaging:17.3.3'
确保您使用的是 pubspec.yaml
中所有云依赖项的最新版本这个解决方案对我有用
只是 运行 flutter clean
和 运行 您的应用程序。
就我而言,我遵循了插件说明https://pub.dev/packages/firebase_messaging 但是,我的 Android 应用还是崩溃了。
原因在文件中android/app/src/main/kotlin/com/example/app/MainActivity.kt
当我把它改成
package YOUR.APP.PACKAGE
import io.flutter.embedding.android.FlutterActivity
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
class Application : FlutterApplication(), PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
override fun registerWith(registry: PluginRegistry?) {
io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}
}
崩溃消失了。
同样为了以防万一,监听即将到来的推送的代码,重要的是要有 onBackgroundMessage
static Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async { print(message); return Future<void>.value(); }
firebaseMessaging.configure(
// when app is open
onMessage: (Map<String, dynamic> message) async {
try {
Get.defaultDialog(
title: message['notification']['title'] ?? 'New reaction to comment',
middleText: message['notification']['body'] ?? 'Click to react',
textCancel: 'Cancel',
textConfirm: 'See',
onCancel: () {},
onConfirm: () => PushServices.managePushesLinkToProject(message));
} catch (e) {
print('$e');
}
},
onLaunch: (Map<String, dynamic> message) async {
PushServices.managePushesLinkToProject(message);
},
onResume: (Map<String, dynamic> message) async {
PushServices.managePushesLinkToProject(message);
},
onBackgroundMessage: myBackgroundMessageHandler,
);
这里找到了关键的解决方案https://github.com/FirebaseExtended/flutterfire/issues/2311