Call onMessage method when the app is in background in flutter(当应用程序在后台运行时调用 onMessage 方法)
问题描述
我是颤振和飞镖的新手.我正在尝试将我的应用与 FCM一个>.当应用程序在前台时,我创建了 flutterLocalNotificationsPlugin 并且一切正常,但是当我的应用程序在后台时,我不知道如何处理 onMessage 方法.有人知道我该如何解决吗?
I'm new in flutter and dart. I'm trying to connect my app with FCM. When app is in foreground I create flutterLocalNotificationsPlugin and everything works fine, but I don't how to handle onMessage method when my app is in background. Have somebody any idea how I can resolve it?
FirebaseMessaging firebaseMessaging = new FirebaseMessaging();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
@override
void initState() {
super.initState();
var androidInitSettings = new AndroidInitializationSettings('mipmap/ic_launcher');
var iosInitSettings = new IOSInitializationSettings();
var initSettings = new InitializationSettings(androidInitSettings, iosInitSettings);
flutterLocalNotificationsPlugin.initialize(initSettings, selectNotification: onSelectNotification);
firebaseMessaging.configure(
onLaunch: (Map<String, dynamic> msg) {
print(" onLaunch called ${(msg)}");
},
onResume: (Map<String, dynamic> msg) {
print(" onResume called ${(msg)}");
},
onMessage: (Map<String, dynamic> msg) {
showNotification(msg);
print(" onMessage called ${(msg)}");
},
);
firebaseMessaging.requestNotificationPermissions(const IosNotificationSettings(sound: true, alert: true, badge: true));
firebaseMessaging.onIosSettingsRegistered.listen((IosNotificationSettings setting) {
print('IOS Setting Registed');
});
firebaseMessaging.getToken().then((token) {
update(token);
});
}
推荐答案
根据上一个插件 Firebase Cloud Messaging for Flutter 版本 4.0.0+1,当你在控制台或表单上创建或编译您的推送通知确保包含
click_action: FLUTTER_NOTIFICATION_CLICK
定位 Android 设备时作为自定义数据"键值对(在高级选项"下).此选项在您的应用处于后台状态时启用 onResume.
as a "Custom data" key-value-pair (under "Advanced options") when targeting an Android device.
This option enabling the onResume when your app is in background state.
这篇关于当应用程序在后台运行时调用 onMessage 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当应用程序在后台运行时调用 onMessage 方法
基础教程推荐
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
