当应用程序在后台运行时调用 onMessage 方法 [英] Call onMessage method when the app is in background in flutter

查看:34
本文介绍了当应用程序在后台运行时调用 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 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆