[英]firebase_messaging callbacks don't fire when in foreground . iOS app Flutter
I'm porting my Swift app to Flutter and I'm setting up the FCM using firebase_messaging
package.我正在将我的 Swift 应用程序移植到 Flutter,我正在使用firebase_messaging
包设置 FCM。 When using my Swift app if I send a test message from Firebase console either directly to token or broadly, and I get the notification delivered to the system tray whether the app is in foreground or in background.在使用我的 Swift 应用程序时,如果我从 Firebase 控制台直接向令牌发送测试消息或广泛发送测试消息,并且无论应用程序是在前台还是在后台,我都会收到发送到系统托盘的通知。
This doesn't happen with the Flutter app. Flutter 应用程序不会发生这种情况。 When App is in background I still get both notification in the system tray, but when in foreground none of the onMessage
, onLaunch
or onResume
callbacks gets called.当 App 在后台时,我仍然在系统托盘中收到两个通知,但是在前台时没有onMessage
、 onLaunch
或onResume
回调被调用。
Can you spot what I'm doing wrong?你能看出我做错了什么吗? Many thanks.非常感谢。
class _MapScreenState extends State<MapScreen> {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
String _token;
String _launchMessage;
@override
void initState() {
super.initState();
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
},
onResume: (Map<String, dynamic> message) async {
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message) async {
print('on launch $message');
},
);
_firebaseMessaging.getToken().then((token) {
print('token: $token');
if (mounted) {
setState(() {
_token = token;
});
} else {
_token = token;
}
});
}
Ok, I found the problem being the package itself.好的,我发现问题出在包本身上。 When switching to MaikuB fork of the plugin it all works as expected.当切换到插件的 MaikuB 分支时,一切都按预期工作。
# firebase_messaging: 6.0.9 # doesn't work
firebase_messaging: # works properly
git:
url: https://github.com/MaikuB/flutterfire.git
path: packages/firebase_messaging
He told me that this is been merged to the official repo so it's due an update of the plugin that saves this.他告诉我这已合并到官方存储库中,因此应该更新插件以保存它。 MaikuB's fork actually solves incompatibility with other plugins that need to register to MaikuB的fork其实解决了其他插件不兼容需要注册的问题
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
as his own flutter_local_notification
plugin , used to show local notification.作为他自己的flutter_local_notification
插件,用于显示本地通知。
I hope that this can help other while a new version of the plugin is rolled out, as making it work has been quite frustrating..我希望在推出新版本的插件时这可以帮助其他人,因为让它工作非常令人沮丧..
Cheers干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.