如何解决Flutter 2.0 与 Firebase Cloud Messaging:在 Android 上未调用 onMessage
我在 Flutter 2.0 中遇到了 Firebase 云消息传递 onMessage 的问题。
FirebaseMessaging.onMessage.listen((RemoteMessage message) { ... }
未调用。但是,日志说
收到消息广播
在收到第一条消息时,我收到了额外的警告,例如:
访问隐藏方法Landroid/os/WorkSource
但是警告在后续消息中消失了。
有趣的事情就是
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
有效。
代码
@override
void initState() {
initializeFlutterFire()
.then((value) => subscribetoMessages);
super.initState();
}
Future<void> initializeFlutterFire() async {
try {
Firebase.initializeApp();
setState(() {
_initialized = true;
print("Firebase has been initialized");
});
} catch (e) {
setState(() {
_error = true;
});
}
}
void subscribetoMessages() {
// The following handler is called,when App is in the background.
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
// The following function is not called,when a message was received by the device.
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
}
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print('message from background handler');
print("Handling a background message: ${message.messageId}");
}
我的感觉是,没有来自 onMessage().listen(...) 的订阅,这就是为什么什么都不执行。
你有什么建议吗,我做错了什么?
提前致谢, 乌维
环境
Firebase 控制台云消息传递
开
华为 P20 和三星 galaxy Tab A 10,均在 Android 10 上运行。
pubspec.yaml
firebase_core: "^1.0.1"
firebase_messaging: "^9.0.0"
android/build.gradle
dependencies {
...
classpath 'com.google.gms:google-services:4.3.5'
android/app/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
...
android {
compileSdkVersion 30
...
defaultConfig {
minSdkVersion 16
targetSdkVersion 30
...
AndroidManifest.xml
<activity ...>
...
<intent-filter>
<action android:name="FlutteR_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</activity>
扑医生
Doctor summary (to see all details,run Flutter doctor -v):
[✓] Flutter (Channel beta,2.0.2,on macOS 11.2.1 20D75 darwin-x64,locale de)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[!] Xcode - develop for iOS and macOS
✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
Download at: https://developer.apple.com/xcode/download/
Or install Xcode via the App Store.
Once installed,run:
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
sudo xcodebuild -runFirstLaunch
[✓] Chrome - develop for the web
[✓] Android Studio
[✓] Android Studio (version 4.1)
[✓] IntelliJ IDEA Community Edition (version 2020.1.2)
[✓] VS Code (version 1.53.0)
[✓] Connected device (3 available)
! Doctor found issues in 1 category.
解决方法
有一个带有 firebase_messaging 9.0.0 插件的 issue。
如果您使用 Cmd+Click 或 Ctrl+Click 导航到工厂 RemoteMessage.fromMap() 的定义,同时将鼠标悬停在 RemoteMessage 类上),在 return 语句中,更改
contentAvailable: map['contentAvailable'],mutableContent: map['mutableContent'],
到
contentAvailable: map['contentAvailable'] ?? false,to mutableContent: map['mutableContent'] ?? false,
您可能需要确认您要修改包。这应该可以帮助您完成程序包更新。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。