Skip to content

Commit ee0483c

Browse files
committed
refactor(push-notification): use OneSignalRequestBody model for API request
- Replace manual JSON construction with OneSignalRequestBody model - Simplify request body creation by using a dedicated model class - Improve code readability and maintainability
1 parent d15de37 commit ee0483c

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

lib/src/services/onesignal_push_notification_client.dart

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:core/core.dart';
2+
import 'package:flutter_news_app_api_server_full_source_code/src/models/models.dart';
23
import 'package:flutter_news_app_api_server_full_source_code/src/services/push_notification_client.dart';
34
import 'package:http_client/http_client.dart';
45
import 'package:logging/logging.dart';
@@ -94,26 +95,19 @@ class OneSignalPushNotificationClient implements IPushNotificationClient {
9495
// app_dependencies.dart. The final URL will be: `https://onesignal.com/api/v1/notifications`
9596
const url = 'notifications';
9697

97-
// Construct the OneSignal API request body.
98-
final requestBody = {
99-
'app_id': appId,
100-
'include_player_ids': deviceTokens,
101-
'headings': {'en': payload.title},
102-
'contents': {'en': payload.title},
103-
if (payload.imageUrl != null) 'big_picture': payload.imageUrl,
104-
// Reconstruct the data payload from the explicit fields
105-
'data': {
106-
'notificationId': payload.notificationId,
107-
'notificationType': payload.notificationType.name,
108-
'contentType': payload.contentType.name,
109-
'contentId': payload.contentId,
110-
},
111-
};
112-
11398
_log.finer(
11499
'Sending OneSignal batch of ${deviceTokens.length} notifications.',
115100
);
116101

102+
final requestBody = OneSignalRequestBody(
103+
appId: appId,
104+
includePlayerIds: deviceTokens,
105+
headings: {'en': payload.title},
106+
contents: {'en': payload.title},
107+
bigPicture: payload.imageUrl,
108+
data: payload,
109+
);
110+
117111
try {
118112
// The OneSignal API returns a JSON object with details about the send,
119113
// including errors for invalid player IDs.

0 commit comments

Comments
 (0)