Skip to content

Commit d7366ed

Browse files
committed
feat(models): add push notification request models
- Add FirebaseRequestBody and OneSignalRequestBody models - Include necessary JSON serialization and equity comparison - Export new models from push_notification.dart
1 parent ffd81d0 commit d7366ed

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

lib/src/models/models.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export 'push_notification/push_notification.dart';
2+
export 'request_id.dart';
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import 'package:equatable/equatable.dart';
2+
import 'package:json_annotation/json_annotation.dart';
3+
4+
part 'firebase_request_body.g.dart';
5+
6+
/// {@template firebase_request_body}
7+
/// Represents the top-level structure for a Firebase Cloud Messaging
8+
/// v1 API request.
9+
/// {@endtemplate}
10+
@JsonSerializable(explicitToJson: true, includeIfNull: true, checked: true)
11+
class FirebaseRequestBody extends Equatable {
12+
/// {@macro firebase_request_body}
13+
const FirebaseRequestBody({required this.message});
14+
15+
/// The message payload.
16+
final FirebaseMessage message;
17+
18+
/// Converts this [FirebaseRequestBody] instance to a JSON map.
19+
Map<String, dynamic> toJson() => _$FirebaseRequestBodyToJson(this);
20+
21+
@override
22+
List<Object> get props => [message];
23+
}
24+
25+
/// {@template firebase_message}
26+
/// Represents the message object within a Firebase request.
27+
/// {@endtemplate}
28+
@JsonSerializable(explicitToJson: true, includeIfNull: true, checked: true)
29+
class FirebaseMessage extends Equatable {
30+
/// {@macro firebase_message}
31+
const FirebaseMessage({
32+
required this.token,
33+
required this.notification,
34+
required this.data,
35+
});
36+
37+
/// The registration token of the device to send the message to.
38+
final String token;
39+
40+
/// The notification content.
41+
final FirebaseNotification notification;
42+
43+
/// The custom data payload.
44+
final Map<String, dynamic> data;
45+
46+
/// Converts this [FirebaseMessage] instance to a JSON map.
47+
Map<String, dynamic> toJson() => _$FirebaseMessageToJson(this);
48+
49+
@override
50+
List<Object> get props => [token, notification, data];
51+
}
52+
53+
/// {@template firebase_notification}
54+
/// Represents the notification content within a Firebase message.
55+
/// {@endtemplate}
56+
@JsonSerializable(explicitToJson: true, includeIfNull: true, checked: true)
57+
class FirebaseNotification extends Equatable {
58+
/// {@macro firebase_notification}
59+
const FirebaseNotification({
60+
required this.title,
61+
required this.body,
62+
this.image,
63+
});
64+
65+
/// The notification's title.
66+
final String title;
67+
68+
/// The notification's body text.
69+
final String body;
70+
71+
/// The URL of an image to be displayed in the notification.
72+
final String? image;
73+
74+
/// Converts this [FirebaseNotification] instance to a JSON map.
75+
Map<String, dynamic> toJson() => _$FirebaseNotificationToJson(this);
76+
77+
@override
78+
List<Object?> get props => [title, body, image];
79+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import 'package:equatable/equatable.dart';
2+
import 'package:json_annotation/json_annotation.dart';
3+
4+
part 'onesignal_request_body.g.dart';
5+
6+
/// {@template onesignal_request_body}
7+
/// Represents the request body for the OneSignal /notifications endpoint.
8+
/// {@endtemplate}
9+
@JsonSerializable(
10+
explicitToJson: true,
11+
includeIfNull: false, // Do not include null fields in the JSON
12+
checked: true,
13+
fieldRename: FieldRename.snake,
14+
)
15+
class OneSignalRequestBody extends Equatable {
16+
/// {@macro onesignal_request_body}
17+
const OneSignalRequestBody({
18+
required this.appId,
19+
required this.includePlayerIds,
20+
required this.headings,
21+
required this.contents,
22+
required this.data,
23+
this.bigPicture,
24+
});
25+
26+
/// The OneSignal App ID.
27+
final String appId;
28+
29+
/// A list of OneSignal Player IDs to send the notification to.
30+
final List<String> includePlayerIds;
31+
32+
/// The notification's title.
33+
final Map<String, String> headings;
34+
35+
/// The notification's content.
36+
final Map<String, String> contents;
37+
38+
/// The custom data payload.
39+
final Map<String, dynamic> data;
40+
41+
/// The URL of a large image to display in the notification.
42+
final String? bigPicture;
43+
44+
/// Converts this [OneSignalRequestBody] instance to a JSON map.
45+
Map<String, dynamic> toJson() => _$OneSignalRequestBodyToJson(this);
46+
47+
@override
48+
List<Object?> get props => [
49+
appId,
50+
includePlayerIds,
51+
headings,
52+
contents,
53+
data,
54+
bigPicture,
55+
];
56+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export 'firebase_request_body.dart';
2+
export 'onesignal_request_body.dart';

0 commit comments

Comments
 (0)