1

How to send scheduled push notifications using REST API?

sendFSM() async {
  const postUrl = 'https://fcm.googleapis.com/fcm/send';
  Map<String, dynamic> data;
  String time =
      Timestamp.fromDate(DateTime.now().add(const Duration(minutes: 1)))
          .millisecondsSinceEpoch
          .toString();
  data = {
    "registration_ids": [
      "dqR9vyp4R2W9keBUKdd66a:APA91bEuSc8r3bhjz0oZsA9uiM9-SyIttNgnJIujhB7K9OoqXNGFb5doim0CAyXCYRnAreIP5KUAWLLf3Fn6jzD_qptHfgxbZT5dGR75ntAiw-hqoxQTWG2RTei5IIYnOY46ECzLv-QW"
    ],
    "collapse_key": "type_a",
    "notification": {
      "title": "title",
      "body": "body",
      "event_time": time,
    },
    'data': {
      "title": "title",
      "body": "body",
      "event_time": time,
    },
  };

  final response =
      await http.post(Uri.parse(postUrl), body: json.encode(data), headers: {
    'content-type': 'application/json',
    'Authorization':
        "Key=AAA*****************************FCsxhvLO6C7XYCAI-rfPURYBp"
  });

  print(response.body);
  if (response.statusCode == 200) {
    return true;
  } else {
    return false;
  }
}

Result: When I am sending this request I am getting a notification immediately in my system tray. Expected: Receive notification after 1 min as per the code written.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Som Gupta
  • 31
  • 2
  • *firebaser here* Calls to the FCM REST API require that you specify the FCM *server** key in your code. As its name implies, this key should only be used in server-side code, or in an otherwise trusted environment. The reason for this is that anyone who has the FCM server key can send whatever message they want to all of your users. By including this key in your Flutter app, a malicious user can find it and you're putting your users at risk. See https://stackoverflow.com/a/37993724 for a better solution. – Frank van Puffelen Nov 02 '22 at 14:19

0 Answers0