繁体   English   中英

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

[英]Call onMessage method when the app is in background in flutter

我是扑扑和飞镖的新手。 我正在尝试将我的应用程序与FCM连接。 当应用程序处于前台时,我创建了 flutterLocalNotificationsPlugin 并且一切正常,但是当我的应用程序处于后台时,我不知道如何处理 onMessage 方法。 有人知道我该如何解决吗?

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);
 });
}

我看到您在触发onMessage时强行显示通知,如果应用程序在后台,您不需要这样做,通知将自动创建。

当您收到通知并且应用程序打开并在前台运行时触发onMessage 例如,您打开 Gmail 应用程序,并收到一封新电子邮件,在这种情况下,您不需要在通知区域弹出通知。 应用程序可能会选择直接处理它,并且在收到通知后立即触发onMessage - 这很好,因此您无需继续池化服务器。

onResumeonLaunch有点不同——收到通知时不会触发这两个事件。 只有当用户从通知区域选择/点击通知时才会触发它们。 因此,在这两种情况下,应用程序当前都处于隐藏状态,要么根本不运行(终止),要么应用程序在后台 - 未显示。 在这种情况下,通知在手机中接收并自动放置在通知区域中(您不需要为此编码“ showNotification ”)。 在这种状态下,用户可以看到通知,但应用程序本身还没有意识到它。

只有当用户选择这些通知之一时,应用程序才会意识到该通知。

如果应用程序根本没有运行,当用户点击通知时将触发onLaunch 这意味着应用程序没有运行,并且由于通知它必须“从头启动”。

如果应用程序在后台,当用户选择通知时将触发onResume将应用程序恢复到前台状态。

编辑:

正如@boformer 所指出的,这仅适用于“通知”消息。 如果您要发送“数据”消息,则不会创建通知,并且消息仅通过onMessage传送。 插件自述文件firebase 文档更多详细信息。

根据Flutter版本 4.0.0+1 的最后一个插件Firebase Cloud Messaging ,当您在控制台或表单上创建或编译推送通知时,请确保包含

click_action: FLUTTER_NOTIFICATION_CLICK 

当定位到 Android 设备时,作为“自定义数据”键值对(在“高级选项”下)。 当您的应用程序处于后台状态时,此选项启用onResume

当应用程序在后台时,Dart VM 不会运行。 这意味着您必须在本机代码(Java/Kotlin/ObjectiveC/Swift)中处理通知和数据消息。

要在 Android 上执行此操作,请参阅官方文档

您可能必须删除 firebase_messaging 插件并手动完成所有消息处理。 要将通知内容发送到您的 Flutter 应用程序(当它在前台时),请使用平台通道。

查看 firebase_messaging 插件的源代码以了解本机端发生的情况确实很有帮助。

要在后台处理消息,请将 firebase-messaging 实现添加到依赖项括号。

Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
  if (message.containsKey('data')) {
    // Handle data message
    final dynamic data = message['data'];
  }

  if (message.containsKey('notification')) {
    // Handle notification message
    final dynamic notification = message['notification'];
  }

  // Or do other work.
}

并在onBackgroundMessage上的配置中调用它

 _firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) async {
    print("onMessage: $message");
    _showItemDialog(message);
  },
  onBackgroundMessage: myBackgroundMessageHandler,
  onLaunch: (Map<String, dynamic> message) async {
    print("onLaunch: $message");
    _navigateToItemDetail(message);
  },
  onResume: (Map<String, dynamic> message) async {
    print("onResume: $message");
    _navigateToItemDetail(message);
  },
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM