push_notifications_service.dart 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import 'package:flutter/foundation.dart';
  2. import 'package:http/http.dart' as http;
  3. class PushNotificationsService {
  4. static const _url = 'https://physigo.vercel.app/api/push-notification';
  5. static void sendFriendRequestNotification(String userId, String name, String surname) {
  6. const title = "Friend Request";
  7. final body = "$name $surname wants to be your friend";
  8. _sendPushNotification(userId, title, body);
  9. }
  10. static void sendChallengeLocationRequestNotification(String userId, String name, String surname) {
  11. const title = "Shared Challenge location request";
  12. final body = "$name $surname shared their challenge's location with you";
  13. _sendPushNotification(userId, title, body);
  14. }
  15. static void sendFriendRequestAcceptedNotification(String userId, String name, String surname) {
  16. const title = "New Friend";
  17. final body = "$name $surname is now your friend!";
  18. _sendPushNotification(userId, title, body);
  19. }
  20. static Future<void> _sendPushNotification(String userId, String title, String body) async {
  21. try {
  22. await http.post(Uri.parse('$_url?user_id=$userId&title=$title&body=$body'));
  23. } catch (error) {
  24. // We don't care if sending the notification fails
  25. if (kDebugMode) {
  26. debugPrint(error.toString());
  27. }
  28. }
  29. }
  30. }