sendNotification method
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;
}
}