[英]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.