feat: implement testing notification on invitation acceptance
This commit is contained in:
parent
3c9127ea17
commit
f55491022f
@ -275,6 +275,66 @@ class NotificationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<bool> sendNotificationToToken(
|
||||||
|
String token,
|
||||||
|
String title,
|
||||||
|
String body, {
|
||||||
|
Map<String, String>? data,
|
||||||
|
}) async {
|
||||||
|
try {
|
||||||
|
final accessToken = await _getAccessToken();
|
||||||
|
if (accessToken == null) {
|
||||||
|
print('Failed to get access token. Cannot send notifications.');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
final url = Uri.parse('https://fcm.googleapis.com/v1/projects/$_projectId/messages:send');
|
||||||
|
final headers = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': 'Bearer $accessToken',
|
||||||
|
};
|
||||||
|
|
||||||
|
final payload = {
|
||||||
|
'message': {
|
||||||
|
'token': token,
|
||||||
|
'notification': {
|
||||||
|
'title': title,
|
||||||
|
'body': body,
|
||||||
|
},
|
||||||
|
'data': data ?? {},
|
||||||
|
'webpush': {
|
||||||
|
'notification': {
|
||||||
|
'icon': 'icons/ios/192.png',
|
||||||
|
'badge': 'icons/ios/192.png',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
print('Sending notification to token: ${token.substring(0, 20)}...');
|
||||||
|
print('Payload: ${json.encode(payload)}');
|
||||||
|
|
||||||
|
final response = await http.post(
|
||||||
|
url,
|
||||||
|
headers: headers,
|
||||||
|
body: json.encode(payload),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
print('Notification sent successfully to token!');
|
||||||
|
print('Response: ${response.body}');
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
print('Failed to send notification. Status: ${response.statusCode}');
|
||||||
|
print('Response: ${response.body}');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print('Error sending notification: $e');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<bool> sendNotificationToTopic(
|
Future<bool> sendNotificationToTopic(
|
||||||
String topic,
|
String topic,
|
||||||
String title,
|
String title,
|
||||||
@ -333,8 +393,11 @@ class NotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> sendCoffeeInviteAcceptedNotification() async {
|
Future<bool> sendCoffeeInviteAcceptedNotification() async {
|
||||||
return await sendNotificationToTopic(
|
// Send to specific token for testing
|
||||||
'all',
|
const testToken = 'cetNA4PmdzwBS075-zdvbP:APA91bEDAqLC0D4txAaiYAdzzfk43yk99tKlC_x3Tii24qHdyhIzuQifsJG4Rm_oijvdVkbsoQUm3tQso34_71OvU-kQsqZTrMOQVIvHkIul7PYqOODFJUA';
|
||||||
|
|
||||||
|
return await sendNotificationToToken(
|
||||||
|
testToken,
|
||||||
'Coffee Time! ☕',
|
'Coffee Time! ☕',
|
||||||
'You\'re set for coffee! Your invitation has been accepted.',
|
'You\'re set for coffee! Your invitation has been accepted.',
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user