import 'dart:convert'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:didvan/models/notification_message.dart'; import 'package:didvan/services/app_home_widget/home_widget_repository.dart'; import 'package:didvan/services/app_initalizer.dart'; import 'package:didvan/services/storage/storage.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/foundation.dart'; import 'package:get/get.dart'; class NotificationService { static final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); static Future initializeNotification() async { const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher'); const InitializationSettings initializationSettings = InitializationSettings(android: initializationSettingsAndroid); await _flutterLocalNotificationsPlugin.initialize( initializationSettings, onDidReceiveNotificationResponse: _onNotificationResponse, ); const AndroidNotificationChannel channel = AndroidNotificationChannel( 'content', // id 'Content Notification', // name description: 'Notification channel', // description importance: Importance.max, playSound: true, ); await _flutterLocalNotificationsPlugin .resolvePlatformSpecificImplementation< AndroidFlutterLocalNotificationsPlugin>() ?.createNotificationChannel(channel); } static Future _onNotificationResponse( NotificationResponse response) async { try { final payload = response.payload; if (payload != null) { NotificationMessage data = NotificationMessage.fromJson(jsonDecode(payload)); HomeWidgetRepository.data = data; if (kDebugMode) { print("data: ${HomeWidgetRepository.data}"); } await HomeWidgetRepository.decideWhereToGoNotif(); await StorageService.delete( key: 'notification${AppInitializer.createNotificationId(data)}'); } } catch (e) { e.printError(); } } static Future showFirebaseNotification(RemoteMessage message) async { try { final data = NotificationMessage.fromJson(message.data); if (data.notificationType!.contains('3')) { HomeWidgetRepository.fetchWidget(); return; } NotificationService.showNotification(data); } catch (e) { e.printError(); } } static Future showNotification(NotificationMessage data) async { if (data.notificationType!.contains('2')) { final String? storedValue = await StorageService.getValue( key: 'notification${AppInitializer.createNotificationId(data)}'); if (storedValue != null) { _flutterLocalNotificationsPlugin .cancel(AppInitializer.createNotificationId(data)); data.body = '${data.body}\n$storedValue'; } } final AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails( 'content', // channel id 'Content Notification', // channel name channelDescription: 'Notification channel', // channel description importance: Importance.max, priority: Priority.high, playSound: true, styleInformation: data.notificationType!.contains('1') ? BigPictureStyleInformation( FilePathAndroidBitmap(data.image ?? ''), contentTitle: data.title, summaryText: "خبر جدید", ) : MessagingStyleInformation( Person(name: data.title), messages: [Message(data.body ?? '', DateTime.now(), null)], ), ); final NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics); await _flutterLocalNotificationsPlugin.show( data.notificationType!.contains('1') ? DateTime.now().millisecondsSinceEpoch ~/ 1000 : AppInitializer.createNotificationId(data), data.title, data.body, platformChannelSpecifics, payload: data.toPayload().toString(), ); if (data.notificationType!.contains('2')) { await StorageService.setValue( key: 'notification${AppInitializer.createNotificationId(data)}', value: data.body); } } }