· 5 months ago · May 04, 2025, 08:00 AM
1class NotifServices {
2 // OneSignal Credentials and API endpoint.
3 static const String _appId = "app-id-nako";
4 static const String _authKey = "Basic auth-key-nako";
5 static const String _onesignalUrl = "https://onesignal.com/api/v1/notifications";
6
7
8 static Future<void> sendGroupNotification({
9 required String notifPref,
10 required String heading,
11 required String content,
12 String? bigPicture,
13 }) async {
14 final Map<String, dynamic> notificationData = {
15 "app_id": _appId,
16 "filters": [
17 {
18 "field": "tag",
19 "key": "notifPref",
20 "relation": "=",
21 "value": notifPref,
22 }
23 ],
24 "headings": {"en": heading},
25 "contents": {"en": content},
26 };
27
28 if (bigPicture != null) {
29 notificationData["big_picture"] = bigPicture;
30 }
31
32 try {
33 final response = await http.post(
34 Uri.parse(_onesignalUrl),
35 headers: {
36 "Content-Type": "application/json; charset=utf-8",
37 "Authorization": _authKey,
38 },
39 body: jsonEncode(notificationData),
40 );
41
42 if (response.statusCode == 200) {
43 print("Group notification sent successfully: ${response.body}");
44 } else {
45 print("Failed to send group notification: ${response.body}");
46 }
47 } catch (e) {
48 print("============================: $notifPref, $heading, $content");
49 print("Exception sending group notification: $e");
50 }
51 }
52
53
54 static Future<void> sendIndividualNotification({
55 required String playerId,
56 required String heading,
57 required String content,
58 String? bigPicture,
59 }) async {
60 final Map<String, dynamic> notificationData = {
61 "app_id": _appId,
62 "include_player_ids": [playerId],
63 "headings": {"en": heading},
64 "contents": {"en": content},
65 };
66
67 if (bigPicture != null) {
68 notificationData["big_picture"] = bigPicture;
69 }
70
71 try {
72 final response = await http.post(
73 Uri.parse(_onesignalUrl),
74 headers: {
75 "Content-Type": "application/json; charset=utf-8",
76 "Authorization": _authKey,
77 },
78 body: jsonEncode(notificationData),
79 );
80
81 if (response.statusCode == 200) {
82 print("Individual notification sent successfully: ${response.body}");
83 } else {
84 print("Failed to send individual notification: ${response.body}");
85 }
86 } catch (e) {
87 print("Exception sending individual notification: $e");
88 }
89 }
90}