sendNotification method

Future<bool> sendNotification({
  1. required String token,
  2. required String title,
  3. required String body,
  4. Map<String, dynamic>? data,
})

Implementation

Future<bool> sendNotification({
  required String token,
  required String title,
  required String body,
  Map<String, dynamic>? data,
}) async {
  try {
    final accessToken = await _getAccessToken();
    final projectId = Handlerz.fc?.projectId;
    if(projectId == null){
      throw Exception('projectId not found');
    }
    final response = await http.post(
      Uri.parse('https://fcm.googleapis.com/v1/projects/$projectId/messages:send'),
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer $accessToken',
      },
      body: jsonEncode({
        'message': {
          'token': token,
          'notification': {
            'title': title,
            'body': body,
          },
          'data': data ?? {},
          // 'android': {
          //   'notification': {
          //     'click_action': 'FLUTTER_NOTIFICATION_CLICK',
          //     'channel_id': 'default_channel',
          //   },
          // },
          // 'apns': {
          //   'payload': {
          //     'aps': {
          //       'category': 'DEFAULT',
          //       'sound': 'default',
          //     },
          //   },
          // },
        },
      }),
    );
    if(kDebugMode){
      print(response.body);
    }

    return response.statusCode == 200;
  } catch (e) {
    if(kDebugMode){
      print('Error sending notification: $e');
    }
    return false;
  }
}