具有Firebase Cloud Messaging的Flutter 2.0:在Android上未调用onMessage
Flutter 2.0中的Firebase Cloud Messaging onMessage出现问题.
I got a problem with Firebase Cloud Messaging onMessage in Flutter 2.0.
功能
FirebaseMessaging.onMessage.listen((RemoteMessage消息){...}
不被呼叫.但是,日志说
收到广播的消息
在收到第一条消息时,我还会收到其他警告,例如:
At the first received message, I get additional warnings, like:
访问隐藏的方法Landroid/os/WorkSource
Accessing hidden method Landroid/os/WorkSource
但是警告在随后的消息中消失.
But the warnings disappear on subsequent messages.
有趣的事情是
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
有效.
如果我将应用程序发送到后台,则会收到通知并调用已定义的方法.
If I send the app to the background, I get a notification and the defined method is called.
@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(...)没有订阅,这就是为什么什么都不执行的原因.
I got the feeling, that there is no subscription from onMessage().listen(...) and that's why nothing is executed.
您有什么建议吗,我在做什么错了?
Do you have any suggestions, what I am doing wrong?
预先感谢,乌韦
Firebase控制台云消息传递
Firebase Console Cloud Messaging
上
华为P20和三星Galaxy Tab A 10,均在Android 10上运行.
Huawei P20 and Samsung Galaxy Tab A 10, both on Android 10.
pubspec.yaml
pubspec.yaml
firebase_core: "^1.0.1"
firebase_messaging: "^9.0.0"
android/build.gradle
android/build.gradle
dependencies {
...
classpath 'com.google.gms:google-services:4.3.5'
android/app/build.gradle
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
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插件.
There is an issue with firebase_messaging 9.0.0 plugin.
如果在用鼠标悬停在RemoteMessage类上时使用Cmd + Click或Ctrl + Click导航到工厂RemoteMessage.fromMap()的定义,请在return语句中更改
If you navigate to the definition of the factory RemoteMessage.fromMap() Using Cmd+Click or Ctrl+Click while hovering over the RemoteMessage class with the mouse), in the return statement, change
contentAvailable: map['contentAvailable'],
mutableContent: map['mutableContent'],
到
contentAvailable: map['contentAvailable'] ?? false,
to mutableContent: map['mutableContent'] ?? false,
您可能必须确认要修改软件包.这将使您逐步了解,直到软件包更新为止.
You may have to confirm that you want to modify the package. This should get you through until the package is updated.