@@ -3,130 +3,9 @@ import 'dart:async';
33import 'package:firebase_core/firebase_core.dart' ;
44import 'package:firebase_messaging/firebase_messaging.dart' ;
55import 'package:flutter/foundation.dart' ;
6- import 'package:flutter_local_notifications/flutter_local_notifications.dart' ;
76import 'package:social_media_app/app_services/notification_service.dart' ;
87import 'package:social_media_app/utils/utility.dart' ;
98
10- @pragma ('vm:entry-point' )
11- int setNotificationId (String type) {
12- switch (type) {
13- case 'Chats' :
14- return 2 ;
15- case 'Followers' :
16- return 3 ;
17- case 'Likes' :
18- return 4 ;
19- case 'Comments' :
20- return 5 ;
21- case 'Follow Requests' :
22- return 6 ;
23- case 'General Notifications' :
24- return 7 ;
25- default :
26- return 1 ;
27- }
28- }
29-
30- @pragma ('vm:entry-point' )
31- Priority setNotificationPriority (String type) {
32- switch (type) {
33- case 'Chats' :
34- return Priority .max;
35- case 'General Notifications' :
36- return Priority .high;
37- case 'Comments' :
38- return Priority .high;
39- case 'Follow Requests' :
40- return Priority .high;
41- case 'Followers' :
42- return Priority .high;
43- case 'Likes' :
44- return Priority .high;
45- default :
46- return Priority .defaultPriority;
47- }
48- }
49-
50- @pragma ('vm:entry-point' )
51- Importance setNotificationImportance (String type) {
52- switch (type) {
53- case 'Chats' :
54- return Importance .max;
55- case 'General Notifications' :
56- return Importance .high;
57- case 'Comments' :
58- return Importance .defaultImportance;
59- case 'Follow Requests' :
60- return Importance .defaultImportance;
61- case 'Followers' :
62- return Importance .defaultImportance;
63- case 'Likes' :
64- return Importance .defaultImportance;
65- default :
66- return Importance .defaultImportance;
67- }
68- }
69-
70- @pragma ('vm:entry-point' )
71- bool setNotificationPlaySound (String type) {
72- switch (type) {
73- case 'Chats' :
74- return true ;
75- case 'General Notifications' :
76- return true ;
77- case 'Comments' :
78- return false ;
79- case 'Follow Requests' :
80- return false ;
81- case 'Followers' :
82- return false ;
83- case 'Likes' :
84- return false ;
85- default :
86- return false ;
87- }
88- }
89-
90- @pragma ('vm:entry-point' )
91- bool setNotificationEnableVibration (String type) {
92- switch (type) {
93- case 'Chats' :
94- return true ;
95- case 'General Notifications' :
96- return true ;
97- case 'Comments' :
98- return false ;
99- case 'Follow Requests' :
100- return false ;
101- case 'Followers' :
102- return false ;
103- case 'Likes' :
104- return false ;
105- default :
106- return false ;
107- }
108- }
109-
110- @pragma ('vm:entry-point' )
111- bool setNotificationEnableLights (String type) {
112- switch (type) {
113- case 'Chats' :
114- return true ;
115- case 'Comments' :
116- return true ;
117- case 'General Notifications' :
118- return true ;
119- case 'Follow Requests' :
120- return true ;
121- case 'Followers' :
122- return true ;
123- case 'Likes' :
124- return true ;
125- default :
126- return false ;
127- }
128- }
129-
1309@pragma ('vm:entry-point' )
13110Future <void > initializeFirebaseService () async {
13211 AppUtility .log ('Initializing Firebase Service' );
@@ -268,37 +147,115 @@ Future<void> initializeFirebaseService() async {
268147// }
269148
270149@pragma ('vm:entry-point' )
271- Future <void > onMessage (RemoteMessage message) async {
272- AppUtility .log ('Got a message whilst in the foreground!' );
273- AppUtility .log ('Message data: ${message .data }' );
150+ bool setNotificationPlaySound (String type) {
151+ switch (type) {
152+ case 'Chats' :
153+ return true ;
154+ case 'General Notifications' :
155+ return true ;
156+ case 'Comments' :
157+ return false ;
158+ case 'Follow Requests' :
159+ return false ;
160+ case 'Followers' :
161+ return false ;
162+ case 'Likes' :
163+ return false ;
164+ default :
165+ return false ;
166+ }
167+ }
274168
169+ @pragma ('vm:entry-point' )
170+ void showNotificationByCategory (
171+ String type, String title, String body, String ? imageUrl) async {
275172 final notificationService = NotificationService ();
276173
277174 if (! notificationService.isInitialized) {
278175 await notificationService.initialize ();
279176 }
280177
178+ switch (type) {
179+ case 'Chats' :
180+ await notificationService.showNotification (
181+ title: title,
182+ body: body,
183+ largeIcon: imageUrl,
184+ channelId: 'Chats' ,
185+ channelName: 'Chats' ,
186+ id: 2 ,
187+ );
188+ break ;
189+ case 'Followers' :
190+ await notificationService.showNotificationWithNoSound (
191+ title: title,
192+ body: body,
193+ largeIcon: imageUrl,
194+ channelId: 'Followers' ,
195+ channelName: 'Followers' ,
196+ id: 3 ,
197+ enableVibration: false ,
198+ );
199+ break ;
200+ case 'Likes' :
201+ await notificationService.showBigPictureNotificationWithNoSound (
202+ title: title,
203+ body: body,
204+ bigPictureUrl: imageUrl ?? '' ,
205+ channelId: 'Likes' ,
206+ channelName: 'Likes' ,
207+ id: 4 ,
208+ enableVibration: false ,
209+ );
210+ break ;
211+ case 'Comments' :
212+ await notificationService.showBigPictureNotification (
213+ title: title,
214+ body: body,
215+ bigPictureUrl: imageUrl ?? '' ,
216+ channelId: 'Comments' ,
217+ channelName: 'Comments' ,
218+ id: 5 ,
219+ );
220+ break ;
221+ case 'Follow Requests' :
222+ await notificationService.showNotificationWithNoSound (
223+ title: title,
224+ body: body,
225+ largeIcon: imageUrl,
226+ channelId: 'Follow Requests' ,
227+ channelName: 'Follow Requests' ,
228+ id: 6 ,
229+ enableVibration: true ,
230+ );
231+ break ;
232+ default :
233+ await notificationService.showNotification (
234+ title: title,
235+ body: body,
236+ largeIcon: imageUrl,
237+ channelId: 'General Notifications' ,
238+ channelName: 'General Notifications' ,
239+ id: 1 ,
240+ );
241+ break ;
242+ }
243+ }
244+
245+ @pragma ('vm:entry-point' )
246+ Future <void > onMessage (RemoteMessage message) async {
247+ AppUtility .log ('Got a message whilst in the foreground!' );
248+ AppUtility .log ('Message data: ${message .data }' );
249+
281250 if (message.data.isNotEmpty) {
282251 var messageData = message.data;
283252
284- var title = messageData['title' ];
285- var body = messageData['body' ];
286- var imageUrl = messageData['image' ];
253+ var title = messageData['title' ] ?? '' ;
254+ var body = messageData['body' ] ?? '' ;
255+ var imageUrl = messageData['image' ] ?? '' ;
287256 var type = messageData['type' ];
288257
289- notificationService.showNotification (
290- title: title ?? '' ,
291- body: body ?? '' ,
292- priority: setNotificationPriority (type),
293- importance: setNotificationImportance (type),
294- playSound: setNotificationEnableLights (type),
295- enableVibration: setNotificationEnableVibration (type),
296- enableLights: setNotificationEnableLights (type),
297- id: setNotificationId (type),
298- largeIcon: imageUrl,
299- channelId: type ?? 'General Notifications' ,
300- channelName: type ?? 'General notifications' ,
301- );
258+ showNotificationByCategory (type, title, body, imageUrl);
302259 }
303260
304261 if (message.notification != null ) {
@@ -312,33 +269,15 @@ Future<void> onBackgroundMessage(RemoteMessage message) async {
312269 debugPrint ("Handling a background message" );
313270 debugPrint ('Message data: ${message .data }' );
314271
315- final notificationService = NotificationService ();
316-
317- if (! notificationService.isInitialized) {
318- await notificationService.initialize ();
319- }
320-
321272 if (message.data.isNotEmpty) {
322273 var messageData = message.data;
323274
324- var title = messageData['title' ];
325- var body = messageData['body' ];
326- var imageUrl = messageData['image' ];
275+ var title = messageData['title' ] ?? '' ;
276+ var body = messageData['body' ] ?? '' ;
277+ var imageUrl = messageData['image' ] ?? '' ;
327278 var type = messageData['type' ];
328279
329- notificationService.showNotification (
330- title: title ?? '' ,
331- body: body ?? '' ,
332- priority: setNotificationPriority (type),
333- importance: setNotificationImportance (type),
334- playSound: setNotificationEnableLights (type),
335- enableVibration: setNotificationEnableVibration (type),
336- enableLights: setNotificationEnableLights (type),
337- id: setNotificationId (type),
338- largeIcon: imageUrl,
339- channelId: type ?? 'General Notifications' ,
340- channelName: type ?? 'General notifications' ,
341- );
280+ showNotificationByCategory (type, title, body, imageUrl);
342281 }
343282
344283 if (message.notification != null ) {
0 commit comments