【发布时间】:2019-09-25 06:25:44
【问题描述】:
当应用程序在前台时我会收到通知,但当应用程序在后台时不会。此外,我已经 google-ed/StackOverflow-ed 了大约 2 小时或更长时间,但能够解决这个问题。
我的配置是:
firebase_auth: ^0.10.0
firebase_messaging: ^5.0.0
清单是这样的:
代码是这样的:
final notifications = new FirebaseMessaging();
class AppNotifications {
static String fcmToken = '';
static Future<Null> init() async {
appLogs("AppNotifications init");
notifications.requestNotificationPermissions(const IosNotificationSettings(sound: true, badge: true, alert: true));
await configure();
fcmToken = await notifications.getToken();
appLogs("FCM TOKEN : " + fcmToken);
notifications.onTokenRefresh.listen((newToken) {
fcmToken = newToken;
appLogs("FCM TOKEN onTokenRefresh: " + fcmToken);
});
await updateFCMToken();
}
static Future<Null> configure() async {
appLogs("AppNotifications Configure");
notifications.configure(onMessage: (msg) {
appLogs('FCM onMessage: ' + msg.toString());
}, onLaunch: (lun) {
appLogs('FCM onLaunch: ' + lun.toString());
}, onResume: (res) {
appLogs('FCM onResume: ' + res.toString());
});
}
static Future<Null> updateFCMToken() async {
auth.currentUser.fcmToken = fcmToken;
await updateUserInSharedPreference();
}
}
我这样调用函数:
class HomeScreenState extends State<HomeScreen> {
@override
void initState() {
super.initState();
Future.delayed(Duration(milliseconds: 100), () async {
await AppNotifications.init();
});
}
..... ....
我正在使用邮递员发送通知:
我的日志:
**(App is Foreground)** I/flutter (31888): APPLOGS : FCM onMessage: {notification: {title: Test notification title, body: Test notification body}, data: {status: done, id: 1, foo: bar, click_action: FLUTTER_NOTIFICATION_CLICK}}
**(App is Background)** W/FirebaseMessaging(31888): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used.
颤振医生:
[✓] Flutter (Channel stable, v1.2.1, on Mac OS X 10.14.4 18E226, locale en-GB)
• Flutter version 1.2.1 at /Users/Ajay/SDK/flutter
• Framework revision 8661d8aecd (3 months ago), 2019-02-14 19:19:53 -0800
• Engine revision 3757390fa4
• Dart version 2.1.2 (build 2.1.2-dev.0.0 0a7dcf17eb)
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at /Users/Ajay/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-28, build-tools 28.0.3
• 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-1248-b01)
• All Android licenses accepted.
[✓] iOS toolchain - develop for iOS devices (Xcode 10.2.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 10.2.1, Build version 10E1001
• ios-deploy 1.9.4
• CocoaPods version 1.6.0
[✓] Android Studio (version 3.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 34.0.1
• Dart plugin version 182.5215
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
[!] VS Code (version 1.33.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
✗ Flutter extension not installed; install from
https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[✓] Connected device (1 available)
• ONEPLUS A5000 • b47e8396 • android-arm64 • Android 9 (API 28)
【问题讨论】:
-
我也在为 Android 上的通知支持而苦苦挣扎。除了应用程序在前台时我没有收到通知。但即使有“后台到达”通知,数据有效负载也会在应用启动时丢失。 iOS 运行得非常好。
-
我终于通过在
runApp(MyApp())中初始化 Firebase 解决了我的 Firebase 消息传递问题。由于您似乎已经这样做了,您的代码流和我的代码流之间的唯一区别是您使用版本5.0.0。你试过降级吗?例如。我用4.0.0+4。 -
@George 当从 API.onLaunch 收到推送通知时,必须单击操作参数,并且未调用 onResume。
-
@AjayKumar 你解决了这个问题吗?
-
如果您针对 Android 设备,建议使用 @ShangariC
click_action。否则,当用户单击托盘中的通知时,不会调用onLaunch/onResume。有关详细信息,请查看文档中的 Sending Messages。