diff --git a/lib/services/notification_service.dart b/lib/services/notification_service.dart index 601d4f1..a544a45 100644 --- a/lib/services/notification_service.dart +++ b/lib/services/notification_service.dart @@ -275,6 +275,66 @@ class NotificationService { } } + Future sendNotificationToToken( + String token, + String title, + String body, { + Map? 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 sendNotificationToTopic( String topic, String title, @@ -333,8 +393,11 @@ class NotificationService { } Future sendCoffeeInviteAcceptedNotification() async { - return await sendNotificationToTopic( - 'all', + // Send to specific token for testing + const testToken = 'cetNA4PmdzwBS075-zdvbP:APA91bEDAqLC0D4txAaiYAdzzfk43yk99tKlC_x3Tii24qHdyhIzuQifsJG4Rm_oijvdVkbsoQUm3tQso34_71OvU-kQsqZTrMOQVIvHkIul7PYqOODFJUA'; + + return await sendNotificationToToken( + testToken, 'Coffee Time! ☕', 'You\'re set for coffee! Your invitation has been accepted.', data: {