当前位置: 首页 > 知识库问答 >
问题:

Flutter Firebase Messaging trigger onLaunch

商经业
2023-03-14

 static final NavigationService _navigationService =
      loc.Locator.locator<NavigationService>();
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

  Future<void> reqPermission() async {
    bool permission = true;
    if (Platform.isIOS) {
      permission = await _firebaseMessaging.requestNotificationPermissions();
    }
    if (permission) {
      _firebaseMessaging.configure(
        onMessage: (Map<String, dynamic> message) async {
          print(message);
          await _navigationService.navigateTo(NewPage.routeName);
        },
        onLaunch: (Map<String, dynamic> message) async {
          print('onLaunch $message');
          await _navigationService.navigateTo(NewPage.routeName);
        },
        onResume: (Map<String, dynamic> message) async {
          print('onResume $message');
          await _navigationService.navigateTo(NewPage.routeName);
        },
        onBackgroundMessage: _myBackgroundHandler,
      );
    }
  }

共有2个答案

斜单鹗
2023-03-14

您可以使用fcm发送通知和数据。它支持终止通知,但不支持数据。您可以在通知中发送正文和标题,因此如果您需要基本通知(只有标题和正文)。您可以使用通知发送。如果您需要更多,可以将其与onLaunch中的flatter_local_notifications包一起使用。https://pub.dev/packages/flutter_local_notifications

这是如何使用代币

_firebaseMessaging.onTokenRefresh.listen((newToken) {
      User _currentUser = FirebaseAuth.instance.currentUser;
      FirebaseFirestore.instance
          .doc("tokens/" + _currentUser.uid)
          .set({"token": newToken});
    });

你可以这样推它。

  Future<bool> sendNotification(
      {@required Map<String, dynamic> messageMap,
      @required AppUser appUser,
      @required String token}) async {
    String url = "https://fcm.googleapis.com/fcm/send";
    String _firebaseKey ="<your key>"

    Map<String, String> headers = {
      "Content-type": "application/json",
      "Authorization": "key=$_firebaseKey"
    };
    String json =
        '{ "to" : "$token", "data" : { "message" : "${messageMap["message"]}", "sendBy": "${appUser.name}", "messageType": "${messageMap["messageType"]}", "sendById" : "${appUser.userId}" } }';
    http.post(url, headers: headers, body: json);
    return true;
  }

FCM配置

_firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        //getNotificationMessage(message["data"]["message"]);
        if(message["data"]["messageType"]=="text"){
          NotificationHandler.showNotification(message); // Flutter Local Notification method
        } else{
          NotificationHandler.showBigPictureNotification(message);// Flutter Local Notification method
        }
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
        if(message["data"]["messageType"]=="text"){
          NotificationHandler.showNotification(message);// Flutter Local Notification method
        } else{
          NotificationHandler.showBigPictureNotification(message);// Flutter Local Notification method
        }
      },
      onResume: (Map<String, dynamic> message) async {
        if(message["data"]["messageType"]=="text"){
          NotificationHandler.showNotification(message);// Flutter Local Notification method
        } else{
          NotificationHandler.showBigPictureNotification(message);// Flutter Local Notification method
        }
      },
      onBackgroundMessage: myBackgroundMessageHandler,
    );
  }

这是后台消息处理程序

Future<void> myBackgroundMessageHandler(Map<String, dynamic> message) async {
  if (message.containsKey('data')) {
    // Handle data message
    final dynamic data = message['data'];
    if(message["data"]["messageType"]=="text"){
      NotificationHandler.showNotification(message);// Flutter Local Notification method
    } else{
      NotificationHandler.showBigPictureNotification(message);// Flutter Local Notification method
    }

  }

  return Future.value();
}

在我的项目中,我是这样做的

柳俊彦
2023-03-14

我最近在GitHub上问了这个问题,请参阅:https://github.com/FirebaseExtended/flutterfire/issues/5098#issuecomment-783444919

旧版本确实如此,它需要一个本机解决方法。最新的非null安全版本firebase_messaging:^8.0.0-dev.14将从终止状态工作(除非您在android上强制退出应用程序)。更新后的留档在https://firebase.flutter.dev/docs/messaging/usage#receiving-messages

 类似资料:

相关问答

相关文章

相关阅读