当应用程序在后台运行时调用 onMessage 方法

Call onMessage method when the app is in background in flutter(当应用程序在后台运行时调用 onMessage 方法)
本文介绍了当应用程序在后台运行时调用 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 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)