简体   繁体   English

在 Flutter 是否可以在 firebase Messaging BackgroundHandler 中调用 API 请求?

[英]In Flutter Is that possible to call API request in firebase Messaging BackgroundHandler?

i have implemented chat message app in which user can reply to chat from push notification when app is killed/background/foreground.我已经实现了聊天消息应用程序,当应用程序被杀死/背景/前景时,用户可以通过推送通知回复聊天。

Future firebaseMessagingBackgroundHandler(RemoteMessage message) async { await GetStorage.init();未来的 firebaseMessagingBackgroundHandler(RemoteMessage message) async { await GetStorage.init(); await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform);等待 Firebase.initializeApp(选项:DefaultFirebaseOptions.currentPlatform); //Call HTTP request sendNotification( personUid, title, body, notificationTypeId, chatRoomId, userTokenDummy, userToken, serverKey, currentUserId, currentUserToken, ); //调用HTTP请求sendNotification(personUid, title, body, notificationTypeId, chatRoomId, userTokenDummy, userToken, serverKey, currentUserId, currentUserToken, ); } }

//Here is a code for API request

  sendNotification({
    required String personUid,
    required String title,
    required String body,
    required int notificationTypeId,
    String? chatRoomId,
    String? userTokenDummy,
    String? userToken,
    String? serverKey,
    String? currentUserId,
    String? currentUserToken,
  }) async {
    try {
      final response = await http.post(
        Uri.parse('https://fcm.googleapis.com/fcm/send'),
        headers: <String, String>{
          HttpHeaders.contentTypeHeader: 
  'application/json',
          HttpHeaders.authorizationHeader: 'key=$serverKey'
        },
        body: jsonEncode(
          <String, dynamic>{
            "data": <String, dynamic>{
              "title": title,
              "body": body,
              "click_action": "FLUTTER_NOTIFICATION_CLICK",
              "id": "1",
              "status": "done",
              "senderId": currentUserId,
              "senderToken": currentUserToken,
              "notificationTypeId": notificationTypeId,
              "chatRoomId": chatRoomId,
            },
            "android": {
              "priority": "high",
            },
            "apns": {
              "headers": {"apns-priority": "10"}
            },
            "to": userToken,
            "content_available": true,
            "mutable-content": 1,
            "priority": "high",
          },
        ),
      );

      return response;
    } catch (e) {
      console(e.toString());
    }
  }

Yes,you can call Http request inside firebaseMessagingBackgroundHandler.是的,您可以在 firebaseMessagingBackgroundHandler 中调用 Http 请求。 But make sure that this api is not taking too much time.但要确保这个 api 没有花费太多时间。 Because long and intensive tasks impacts on device performance.因为长时间和密集的任务会影响设备性能。 Also make sure that there is not exception or error in the api which causes the device to freeze.还要确保 api 中没有导致设备冻结的异常或错误。

To make sure that api is not running forever place a timeout in http class.为确保 api 不会永远运行,请在 http class 中设置超时。

for more refer to firebase documentation: https://firebase.google.com/docs/cloud-messaging/flutter/receive有关更多信息,请参阅 firebase 文档: https://firebase.google.com/docs/cloud-messaging/flutter/receive

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

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