如何使用firebase自动发送FCM消息?

Posted

技术标签:

【中文标题】如何使用firebase自动发送FCM消息?【英文标题】:How to automatically send FCM message in flutter with firebase? 【发布时间】:2022-01-17 23:25:33 【问题描述】:

我正在尝试使用以下代码自动向驱动程序发送通知:

static sendNotificationToDriver(String token, context, String ride_request_id) async
  
    var destionation = Provider.of<AppData>(context, listen: false).dropOffLocation;
    print("this is the token: ");
    print(token);
    Map<String, String> headerMap =
    
      'Content-Type': 'application/json',
      'Authorization': serverToken,
    ;
    print(headerMap);
    Map notificationMap =
    
      'body': 'DropOff Address, $destionation!.placeName',
      'title': 'New Ride Request'
    ;
    print(notificationMap);
    Map dataMap =
    
      'click_action': 'FLUTTER_NOTIFICATION_CLICK',
      'id': '1',
      'status': 'done',
      'ride_request_id': ride_request_id,
    ;
    print(dataMap);
    Map sendNotificationMap =
    
      "notification": notificationMap,
      "data": dataMap,
      "priority": "high",
      "to": token,
    ;
    print(sendNotificationMap);
    var res = await http.post(
      Uri.parse('https://fcm.googleapis.com/fcm/send'),
      headers: headerMap,
      body: jsonEncode(sendNotificationMap),
    );
  

但是,推送通知没有通过。

但是当我用这个代码使用邮递员时:

"notification" : "body" : "New Ride Request! Tap here to view in the app.", 
"title": "New Ride Request", "priority": "high", "data": "click_action": "FLUTTER_NOTIFIATION_CLICK", 
"id": "1", "status": "done", "ride_request_id": "-MqrfzH5DKC8hTYQHy35", 
"to" : "dTlAVB_KTe-XZTLEYwZL10:APA91bF-D-Uh5Yb5p2uCwRr8oT90R_BXOw9qCUGMGsWqlp9RNc-WlyyNfJOKSwPArvAa3062F2XxlUJbY6xibWCYZCDPGqvaQG4xvfgByyyJ9SBxvhAgDmaP35Mqr3dfJADmprMQJ-aL"

它有效。我不知道我做错了什么。

以下是上述打印语句的结果:

I/flutter ( 1288): Content-Type: application/json, Authorization: AAAAUecji90:APA***********************
I/flutter ( 1288): body: DropOff Address, ACE Centerpoint Gen. San. Drive Wing, title: New Ride Request
I/flutter ( 1288): click_action: FLUTTER_NOTIFICATION_CLICK, id: 1, status: done, ride_request_id: -MqrfCLsW3Ncmfav7hdk
I/flutter ( 1288): notification: body: DropOff Address, ACE Centerpoint Gen. San. Drive Wing, title: New Ride Request, data: click_action: FLUTTER_NOTIFICATION_CLICK, id: 1, status: done, ride_request_id: -MqrfCLsW3Ncmfav7hdk, priority: high, to: dTlAVB_KTe-XZTLEYwZL10:APA91bF-D-Uh5Yb5p2uCwRr8oT90R_BXOw9qCUGMGsWqlp9RNc-WlyyNfJOKSwPArvAa3062F2XxlUJbY6xibWCYZCDPGqvaQG4xvfgByyyJ9SBxvhAgDmaP35Mqr3dfJADmprMQJ-aL

【问题讨论】:

你试过在请求数据中不使用jsonEncode吗? 不,我没有尝试不使用它。是否有其他方法可以在应用内实现自动化? 【参考方案1】:

我终于设法让它工作了。我使用了以下代码:

 static sendNotificationToDriver(String token, context, String rideRequestId) async 
    if (token == null) 
      print('Unable to send FCM message, no token exists.');
      return;
    

    try 
      await http.post(
        Uri.parse('https://fcm.googleapis.com/fcm/send'),
        headers: <String, String>
          'Content-Type': 'application/json; charset=UTF-8',
          'Authorization': serverToken,
        ,
        body: constructFCMPayload(token, rideRequestId),
      );
      print('FCM request for device sent!');
     catch (e) 
      print(e);
    
  


static String constructFCMPayload(String token, String rideRequestId) 
  var res = jsonEncode(
    'token': token,
    'notification': 
      "body" : "You have a new ride request! Tap here to view in the app.",
      "title": "New Ride Request"
    ,
    "priority": "high",
    'data': 
      "click_action": "FLUTTER_NOTIFIATION_CLICK",
      "id": "1",
      "status": "done",
      "ride_request_id": rideRequestId,
    ,
    'to': token,

  );

  print(res.toString());
  return res;

但是,问题本身不是代码,而是serverToken 变量。难怪它为什么使用 Postman 而不是在应用程序中工作,因为我忘记将 "key=" 放在 serverToken 值的开头。现在,问题解决了。

【讨论】:

以上是关于如何使用firebase自动发送FCM消息?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Xamarin.iOS 中使用 FCM(Firebase 云消息传递)发送 iOS 推送通知

Flutter:如何使用 fcm 以编程方式发送推送通知

如何使用 FCM(Firebase 云消息传递)制作紧凑通知?

Firebase(FCM)如何获取令牌

Flutter Firebase Cloud Messaging 如何自动关闭/取消通知?

使用 Firebase (FCM) 的丰富通知