我已将firebase-messaging
和flutter_local_notifications
配置为能够在应用程序处于前台和后台时显示显示推送通知。但是,当onMessage
在前台运行时触发时,消息本身似乎为空。
配置:
FirebaseMessaging fcm = FirebaseMessaging();
flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
var initializationSettingsAndroid =
AndroidInitializationSettings('drawable/ic_stat');
var initializationSettingsIOS = IOSInitializationSettings();
var initializationSettings = InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
selectNotification: (String payload) async {});
fcm.configure(
onLaunch: (Map<String, dynamic> msg) {
print("On launch $msg");
showNotification(msg);
},
onResume: (Map<String, dynamic> msg) {
print("On resume $msg");
showNotification(msg);
},
onMessage: (Map<String, dynamic> msg) {
print("On message $msg");
showNotification(msg);
},
);
处理程序:
void showNotification(Map<String, dynamic> msg) async{
print(msg);
var notificationsChannel = new AndroidNotificationDetails(
'my_package',
'my_organization',
'notification_channel',
importance: Importance.Max,
priority: Priority.High);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
notificationsChannel, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, msg['title'], msg['body'], platformChannelSpecifics,);
}
在前台运行应用程序时登录:
W/FirebaseMessaging(10901): Error while parsing timestamp in GCM event
W/FirebaseMessaging(10901): java.lang.NumberFormatException: null
W/FirebaseMessaging(10901): at java.lang.Integer.parseInt(Integer.java:483)
W/FirebaseMessaging(10901): at java.lang.Integer.parseInt(Integer.java:556)
W/FirebaseMessaging(10901): at com.google.firebase.messaging.zzb.zzc(Unknown Source)
W/FirebaseMessaging(10901): at com.google.firebase.messaging.zzb.zze(Unknown Source)
W/FirebaseMessaging(10901): at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source)
W/FirebaseMessaging(10901): at com.google.firebase.iid.zzc.run(Unknown Source)
W/FirebaseMessaging(10901): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
W/FirebaseMessaging(10901): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/FirebaseMessaging(10901): at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source)
W/FirebaseMessaging(10901): at java.lang.Thread.run(Thread.java:761)
W/FirebaseMessaging(10901): Error while parsing timestamp in GCM event
W/FirebaseMessaging(10901): java.lang.NumberFormatException: null
W/FirebaseMessaging(10901): at java.lang.Integer.parseInt(Integer.java:483)
W/FirebaseMessaging(10901): at java.lang.Integer.parseInt(Integer.java:556)
W/FirebaseMessaging(10901): at com.google.firebase.messaging.zzb.zzc(Unknown Source)
W/FirebaseMessaging(10901): at com.google.firebase.messaging.zzb.zzh(Unknown Source)
W/FirebaseMessaging(10901): at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source)
W/FirebaseMessaging(10901): at com.google.firebase.iid.zzc.run(Unknown Source)
W/FirebaseMessaging(10901): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
W/FirebaseMessaging(10901): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/FirebaseMessaging(10901): at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source)
W/FirebaseMessaging(10901): at java.lang.Thread.run(Thread.java:761)
I/flutter (10901): On message {}
I/flutter (10901): {}
E/flutter (10901): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception:
E/flutter (10901): NoSuchMethodError: The method '[]' was called on null.
E/flutter (10901): Receiver: null
E/flutter (10901): Tried calling: []("title")
但是,当应用程序在后台运行时,通知会直接发送到系统托盘,并且它不是空的 (screenshot)。
这是我扑扑的医生的输出:
[✓] Flutter (Channel beta, v0.8.2, on Mac OS X 10.13.5 17F77, locale es-ES)
• Flutter version 0.8.2 at /Users/alex/Documents/Development/flutter
• Framework revision 5ab9e70727 (3 weeks ago), 2018-09-07 12:33:05 -0700
• Engine revision 58a1894a1c
• Dart version 2.1.0-dev.3.1.flutter-760a9690c2
[✓] Android toolchain - develop for Android devices (Android SDK 27.0.3)
• Android SDK at /Users/alex/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-27, build-tools 27.0.3
• ANDROID_HOME = /Users/alex/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
• All Android licenses accepted.
[✓] iOS toolchain - develop for iOS devices (Xcode 9.4.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 9.4.1, Build version 9F2000
• ios-deploy 1.9.2
• CocoaPods version 1.5.3
[✓] Android Studio (version 3.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 26.0.1
• Dart plugin version 173.4700
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
[✓] IntelliJ IDEA Ultimate Edition (version 2018.1.1)
• IntelliJ at /Applications/IntelliJ IDEA.app
• Flutter plugin version 26.0.2
• Dart plugin version 181.4445.29
[✓] VS Code (version 1.27.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 2.18.0
[✓] Connected devices (1 available)
• Android SDK built for x86 • emulator-5554 • android-x86 • Android 7.0 (API 24) (emulator)
• No issues found!
也许这与Firebase消息的工作方式有关?