简体   繁体   English

flutter firebase 消息处理点击后台通知

[英]flutter firebase messaging handling clicked notification on background

I'm using the firebase_messaging package on my flutter app and it works perfectly for everything except that when the app on the background and I get a notification it only opens the app and never do what I ask it does after opening the app..here is my code:我在我的 flutter 应用程序上使用 firebase_messaging package 它适用于所有情况,除了当应用程序在后台并且我收到通知时它只打开应用程序并且从不执行我在打开应用程序后要求它做的事情..这里是我的代码:

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // If you're going to use other Firebase services in the background, such as Firestore,
  // make sure you call `initializeApp` before using other Firebase services.
  await Firebase.initializeApp();

  print("Handling a background message: ${message.messageId}");
  final dynamic data = message.data;
  print("data is $data");
  if(data.containsKey('request_id')) {
    print("we are inside ..!");
    RegExp exp = RegExp(r'\d+');
    var id = int.parse(exp.firstMatch(data['request_id']!)!.group(0)!);
    OpenRequestsController openRequestsController = Get.isRegistered<OpenRequestsController>()?Get.find():Get.put(OpenRequestsController());
    OpenRequest matchingRequest = openRequestsController.openRequestsList.firstWhere((request) => request.id == id);
    print("the request from the list is $matchingRequest");
    openRequestsController.openRequestDetails(matchingRequest,false);

  }
}

what happens here is that it tries to run the whole function when the message is received not when clicked.. and ofc it fails because the app is not already running in the foreground这里发生的是它试图在收到消息时运行整个 function 而不是在单击时..并且它失败了因为应用程序尚未在前台运行

For opening the application and moving to the said screen you need to implement onMessageOpenedApp in the initstate of your first stateful widget which will let the application to know that it is supposed to open the application when a notification being clicked.要打开应用程序并移动到所述屏幕,您需要在第一个有状态小部件的初始状态中实现onMessageOpenedApp ,这将使应用程序知道它应该在单击通知时打开应用程序。 The code function should look like this:代码 function 应如下所示:

FirebaseMessaging.onMessageOpenedApp.listen((message) {
  Get.to(() => const MainScreen()); // add logic here
});

It is better if you could assign identifiers for the type of screens you want to open on clicking a particular notification while sending the notification and implement an if-else or switch statement here on the basis of message type.如果您可以在发送通知时单击特定通知并在此处根据消息类型实施 if-else 或 switch 语句,则可以为要打开的屏幕类型分配标识符。

Thanks谢谢

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

相关问题 FIREBASE_MESSAGING:当应用程序处于后台或已终止时,onBackgroundMessage 不处理通知 - FIREBASE_MESSAGING: onBackgroundMessage not handling notification when app is on Background or Terminated firebase 消息处理与本地通知 - firebase messaging handling with local notification Flutter - Firebase 使用 firebase 消息传递成功但未收到通知的推送通知 - Flutter - Firebase push notification using firebase messaging success but not get notification Flutter Firebase 消息无法点击通知 - Flutter Firebase Messaging cannot click on notification 收到 Firebase 云消息通知时的 Flutter showDialog() - Flutter showDialog() when Firebase Cloud Messaging Notification is received Flutter Firebase 消息 - 应用打开时不显示推送通知 - Flutter Firebase messaging - push notification is not showing when app is open Flutter:如何在 firebase_messaging package 中定义通知通道? - Flutter: How to define notification channel in firebase_messaging package? 如何通过firebase云消息发送定时通知到flutter app - how to send scheduled notification to flutter app through firebase cloud messaging Flutter 推送通知应用程序后台:firebase_messaging - Flutter push notifications app background : firebase_messaging firebase 消息传递和 Firestore 以及 flutter - firebase messaging and firestore and flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM