[英]Notification message seems to be empty when app is on foreground
I have configured firebase-messaging
and flutter_local_notifications
to be able to show show push notifications both when app is in foreground and in background.我已将flutter_local_notifications
firebase-messaging
和flutter_local_notifications
配置为能够在应用程序处于前台和后台时显示推送通知。 However, when onMessage
is triggered with the app on foreground, the message itself seems to be empty.但是,当应用程序在前台触发onMessage
时,消息本身似乎是空的。
Configuration:配置:
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);
},
);
Handler:处理程序:
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,);
}
Log when app in foreground:当应用程序在前台时登录:
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")
However, when the app is in background, the notification is delivered directly to the system tray and it isn't empty (screenshot) .但是,当应用程序在后台时,通知会直接传递到系统托盘,并且它不是空的(截图) 。
Here's my flutter doctor output:这是我的颤振医生输出:
[✓] 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!
Maybe this has something to do with how firebase messages work?也许这与 firebase 消息的工作方式有关?
这是通过将 (1) firebase-messaging
和 (2) flutter_local_notifications
到最新版本来解决的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.