当应用程序在背景中颤动时调用onMessage方法 [英] Call onMessage method when the app is in background in flutter

查看:100
本文介绍了当应用程序在背景中颤动时调用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);
 });
}

推荐答案

根据最新的插件 Flutter的Firebase Cloud Messaging ,当您使用在控制台或表单上创建或编译您的推送通知,确保包含

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天全站免登陆