11part of "push_notification_bloc.dart" ;
22
3+ /// Base notification data that is shared between different notification states
4+ class NotificationData {
5+ /// Notification title
6+ final String title;
7+
8+ /// Notification body
9+ final String body;
10+
11+ /// Notification payload
12+ final PushNotificationPayload payload;
13+
14+ /// Mobile link for the notification
15+ final String ? linkMobile;
16+
17+ /// Creates a notification data instance
18+ const NotificationData ({
19+ required this .title,
20+ required this .body,
21+ required this .payload,
22+ this .linkMobile,
23+ });
24+
25+ /// Converts the notification data to a JSON map
26+ Map <String , dynamic > toJson () {
27+ return {
28+ "title" : title,
29+ "body" : body,
30+ "payload" : payload.toJson (),
31+ "linkMobile" : linkMobile,
32+ };
33+ }
34+
35+ /// Creates a notification data instance from JSON
36+ factory NotificationData .fromJson (Map <String , dynamic > json) {
37+ return NotificationData (
38+ title: json["title" ] ?? "" ,
39+ body: json["body" ] ?? "" ,
40+ payload: PushNotificationPayload .fromJson (json["payload" ] ?? {}),
41+ linkMobile: json["linkMobile" ],
42+ );
43+ }
44+
45+ /// Creates a copy of this notification data with the given fields replaced
46+ NotificationData copyWith ({
47+ String ? title,
48+ String ? body,
49+ PushNotificationPayload ? payload,
50+ String ? linkMobile,
51+ }) {
52+ return NotificationData (
53+ title: title ?? this .title,
54+ body: body ?? this .body,
55+ payload: payload ?? this .payload,
56+ linkMobile: linkMobile ?? this .linkMobile,
57+ );
58+ }
59+
60+ @override
61+ bool operator == (Object other) {
62+ if (identical (this , other)) return true ;
63+ return other is NotificationData &&
64+ other.title == title &&
65+ other.body == body &&
66+ other.payload == payload &&
67+ other.linkMobile == linkMobile;
68+ }
69+
70+ UserNotification toUserNotification () {
71+ return UserNotification ()
72+ ..title.value = title
73+ ..titleWeb.value = title
74+ ..titleMobile.value = title
75+ ..content.value = body
76+ ..contentWeb.value = body
77+ ..contentMobile.value = body
78+ ..link.value = linkMobile
79+ ..linkWeb.value = linkMobile
80+ ..linkMobile.value = linkMobile;
81+ }
82+
83+ @override
84+ int get hashCode {
85+ return Object .hash (title, body, payload, linkMobile);
86+ }
87+ }
88+
389/// Push notification state
490sealed class PushNotificationState {
591 const PushNotificationState ();
692
7- /// Converts the PushNotificationOpened state to a JSON map.
93+ /// Converts the state to a JSON map.
894 ///
995 /// **Returns:**
1096 /// - A Map\<String, dynamic\> representing the state in JSON format.
@@ -18,82 +104,117 @@ final class PushNotificationInitial extends PushNotificationState {
18104
19105 @override
20106 Map <String , dynamic > toJson () {
21- return {};
107+ return {"type" : "initial" };
22108 }
23109}
24110
25111/// Push notification received state
26112final class PushNotificationReceived extends PushNotificationState {
27- /// Push notification received state
28- final String title ;
113+ /// Notification data
114+ final NotificationData data ;
29115
30116 /// Push notification received state
31- final String body;
32-
33- /// Push notification received state
34- final PushNotificationPayload payload;
35-
36- /// Push notification received state
37- final String ? linkMobile;
38-
39- /// Push notification received state
40- PushNotificationReceived ({
41- required this .title,
42- required this .body,
43- required this .payload,
44- this .linkMobile,
117+ const PushNotificationReceived ({
118+ required this .data,
45119 });
46120
47- /// Push notification opened state
48- DidUserOpenedNotificationEvent opened () {
121+ /// Creates a received state from individual notification fields
122+ factory PushNotificationReceived .fromFields ({
123+ required String title,
124+ required String body,
125+ required PushNotificationPayload payload,
126+ String ? linkMobile,
127+ }) {
128+ return PushNotificationReceived (
129+ data: NotificationData (
130+ title: title,
131+ body: body,
132+ payload: payload,
133+ linkMobile: linkMobile,
134+ ),
135+ );
136+ }
137+
138+ /// Converts to opened notification event
139+ DidUserOpenedNotificationEvent toOpenedEvent () {
49140 return DidUserOpenedNotificationEvent (
50- title: title,
51- body: body,
52- payload: payload,
53- linkMobile: linkMobile,
141+ title: data. title,
142+ body: data. body,
143+ payload: data. payload,
144+ linkMobile: data. linkMobile,
54145 );
55146 }
56147
57148 @override
58149 Map <String , dynamic > toJson () {
59150 return {
60- "title" : title,
61- "body" : body,
62- "payload" : payload.toJson (),
63- "linkMobile" : linkMobile,
151+ "type" : "received" ,
152+ "data" : data.toJson (),
64153 };
65154 }
66155}
67156
68157/// Push notification opened state
69158final class PushNotificationOpened extends PushNotificationState {
70- /// Push notification received state
71- final String title ;
159+ /// Notification data
160+ final NotificationData data ;
72161
73- /// Push notification received state
74- final String body;
162+ /// Push notification opened state
163+ const PushNotificationOpened ({
164+ required this .data,
165+ });
75166
76- /// Push notification received state
77- final PushNotificationPayload payload;
167+ /// Creates an opened state from individual notification fields
168+ factory PushNotificationOpened .fromFields ({
169+ required String title,
170+ required String body,
171+ required PushNotificationPayload payload,
172+ String ? linkMobile,
173+ }) {
174+ return PushNotificationOpened (
175+ data: NotificationData (
176+ title: title,
177+ body: body,
178+ payload: payload,
179+ linkMobile: linkMobile,
180+ ),
181+ );
182+ }
78183
79- /// Push notification received state
80- final String ? linkMobile;
184+ @override
185+ Map <String , dynamic > toJson () {
186+ return {
187+ "type" : "opened" ,
188+ "data" : data.toJson (),
189+ };
190+ }
191+ }
81192
82- /// Push notification opened state
83- const PushNotificationOpened ({
84- required this .title,
85- required this .body,
86- required this .payload,
87- this .linkMobile,
193+ /// Push notification error state
194+ final class PushNotificationError extends PushNotificationState {
195+ /// Error message
196+ final String message;
197+
198+ /// Error code (optional)
199+ final String ? code;
200+
201+ /// Original notification data if available
202+ final NotificationData ? originalData;
203+
204+ /// Push notification error state
205+ const PushNotificationError ({
206+ required this .message,
207+ this .code,
208+ this .originalData,
88209 });
89210
90211 @override
91212 Map <String , dynamic > toJson () {
92213 return {
93- "title " : title ,
94- "body " : body ,
95- "payload " : payload. toJson () ,
96- "linkMobile " : linkMobile ,
214+ "type " : "error" ,
215+ "message " : message ,
216+ "code " : code ,
217+ "originalData " : originalData ? . toJson () ,
97218 };
98219 }
99220}
0 commit comments