chore(group): fix delay show group when switch and convert to relative import
This commit is contained in:
@@ -59,6 +59,5 @@ class MapServices {
|
||||
log("Lỗi khi tìm đường");
|
||||
return [];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,18 +3,20 @@ import 'dart:io';
|
||||
import 'dart:math' as math;
|
||||
import 'package:alarm/alarm.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart' hide NotificationSettings;
|
||||
import 'package:firebase_messaging/firebase_messaging.dart'
|
||||
hide NotificationSettings;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:persistent_bottom_nav_bar_v2/persistent_bottom_nav_bar_v2.dart';
|
||||
import 'package:sfm_app/product/utils/app_logger_utils.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
|
||||
import '../utils/app_logger_utils.dart';
|
||||
import '../../firebase_options.dart';
|
||||
import '../../main.dart';
|
||||
import 'alarm_services.dart';
|
||||
|
||||
class NotificationServices {
|
||||
static final FlutterLocalNotificationsPlugin _notificationsPlugin = FlutterLocalNotificationsPlugin();
|
||||
static final FlutterLocalNotificationsPlugin _notificationsPlugin =
|
||||
FlutterLocalNotificationsPlugin();
|
||||
final FirebaseMessaging _messaging = FirebaseMessaging.instance;
|
||||
AlarmServices alarmServices = AlarmServices();
|
||||
|
||||
@@ -24,25 +26,29 @@ class NotificationServices {
|
||||
}
|
||||
|
||||
Future<void> initializeLocalNotifications() async {
|
||||
try{
|
||||
const AndroidInitializationSettings androidInitializationSettings = AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||
const DarwinInitializationSettings iosInitializationSettings = DarwinInitializationSettings();
|
||||
const InitializationSettings initializationSettings = InitializationSettings(
|
||||
android: androidInitializationSettings,
|
||||
iOS: iosInitializationSettings,
|
||||
);
|
||||
try {
|
||||
const AndroidInitializationSettings androidInitializationSettings =
|
||||
AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||
const DarwinInitializationSettings iosInitializationSettings =
|
||||
DarwinInitializationSettings();
|
||||
const InitializationSettings initializationSettings =
|
||||
InitializationSettings(
|
||||
android: androidInitializationSettings,
|
||||
iOS: iosInitializationSettings,
|
||||
);
|
||||
|
||||
await _notificationsPlugin.initialize(
|
||||
initializationSettings,
|
||||
onDidReceiveNotificationResponse: (NotificationResponse response) {
|
||||
dev.log("Người dùng click thông báo ở foreground với payload: ${response.payload}");
|
||||
handleMessage(response.payload,controller);
|
||||
},
|
||||
);
|
||||
dev.log("Local notifications initialized");
|
||||
}catch(e){
|
||||
dev.log("Error initializing local notifications: $e");
|
||||
}
|
||||
await _notificationsPlugin.initialize(
|
||||
initializationSettings,
|
||||
onDidReceiveNotificationResponse: (NotificationResponse response) {
|
||||
dev.log(
|
||||
"Người dùng click thông báo ở foreground với payload: ${response.payload}");
|
||||
handleMessage(response.payload, controller);
|
||||
},
|
||||
);
|
||||
dev.log("Local notifications initialized");
|
||||
} catch (e) {
|
||||
dev.log("Error initializing local notifications: $e");
|
||||
}
|
||||
}
|
||||
|
||||
void firebaseInit(BuildContext context) {
|
||||
@@ -62,11 +68,13 @@ class NotificationServices {
|
||||
try {
|
||||
if (Platform.isAndroid) {
|
||||
return await _notificationsPlugin
|
||||
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
|
||||
.resolvePlatformSpecificImplementation<
|
||||
AndroidFlutterLocalNotificationsPlugin>()
|
||||
?.requestNotificationsPermission();
|
||||
} else if (Platform.isIOS) {
|
||||
return await _notificationsPlugin
|
||||
.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()
|
||||
.resolvePlatformSpecificImplementation<
|
||||
IOSFlutterLocalNotificationsPlugin>()
|
||||
?.requestPermissions(alert: true, sound: true, badge: true);
|
||||
}
|
||||
return null;
|
||||
@@ -84,13 +92,14 @@ class NotificationServices {
|
||||
final type = message.data['type'] as String? ?? 'normal';
|
||||
|
||||
if (title == null || body == null) {
|
||||
dev.log('Skipping notification: missing title or body', name: 'Notification');
|
||||
dev.log('Skipping notification: missing title or body',
|
||||
name: 'Notification');
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle smoke warning notifications
|
||||
if (type == 'smoke_warning') {
|
||||
await alarmServices.showAlarm(title, body);
|
||||
await alarmServices.showAlarm(title, body);
|
||||
dev.log('Displayed smoke warning notification', name: 'Notification');
|
||||
return;
|
||||
}
|
||||
@@ -100,15 +109,16 @@ class NotificationServices {
|
||||
const channelName = 'High Importance Notification';
|
||||
const channelDescription = 'Channel description';
|
||||
|
||||
final androidChannel = AndroidNotificationChannel(
|
||||
channelId,
|
||||
channelName,
|
||||
importance: Importance.max,
|
||||
description: channelDescription,
|
||||
);
|
||||
// final androidChannel = AndroidNotificationChannel(
|
||||
// channelId,
|
||||
// channelName,
|
||||
// importance: Importance.max,
|
||||
// description: channelDescription,
|
||||
// );
|
||||
|
||||
final androidPlugin = _notificationsPlugin
|
||||
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();
|
||||
final androidPlugin =
|
||||
_notificationsPlugin.resolvePlatformSpecificImplementation<
|
||||
AndroidFlutterLocalNotificationsPlugin>();
|
||||
|
||||
// Delete existing channel to prevent conflicts
|
||||
await androidPlugin?.deleteNotificationChannel(channelId);
|
||||
@@ -180,12 +190,13 @@ class NotificationServices {
|
||||
|
||||
Future<void> setupInteractMessage(PersistentTabController controller) async {
|
||||
// Khi app terminated
|
||||
RemoteMessage? initialMessage = await FirebaseMessaging.instance.getInitialMessage();
|
||||
RemoteMessage? initialMessage =
|
||||
await FirebaseMessaging.instance.getInitialMessage();
|
||||
|
||||
if (initialMessage != null) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
try {
|
||||
handleMessage(initialMessage.data['type'],controller);
|
||||
handleMessage(initialMessage.data['type'], controller);
|
||||
} catch (e, stack) {
|
||||
dev.log("Error handling initial message: $e\n$stack");
|
||||
}
|
||||
@@ -194,14 +205,15 @@ class NotificationServices {
|
||||
// Khi app ở background
|
||||
FirebaseMessaging.onMessageOpenedApp.listen((message) {
|
||||
try {
|
||||
handleMessage(message.data['type'],controller);
|
||||
handleMessage(message.data['type'], controller);
|
||||
} catch (e, stack) {
|
||||
dev.log("Error in onMessageOpenedApp: $e\n$stack");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> showBackgroundOrTerminateNotification(RemoteMessage message) async {
|
||||
Future<void> showBackgroundOrTerminateNotification(
|
||||
RemoteMessage message) async {
|
||||
try {
|
||||
// Early validation of notification data
|
||||
final title = message.data['title'] as String?;
|
||||
@@ -209,7 +221,8 @@ class NotificationServices {
|
||||
final type = message.data['type'] as String? ?? 'normal';
|
||||
|
||||
if (title == null || body == null) {
|
||||
dev.log('Skipping notification: missing title or body', name: 'Notification');
|
||||
dev.log('Skipping notification: missing title or body',
|
||||
name: 'Notification');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -218,7 +231,6 @@ class NotificationServices {
|
||||
const channelName = 'High Importance Notification';
|
||||
const channelDescription = 'Channel description';
|
||||
|
||||
|
||||
// Configure notification details
|
||||
final androidDetails = AndroidNotificationDetails(
|
||||
channelId,
|
||||
@@ -273,10 +285,10 @@ Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
||||
await Alarm.init();
|
||||
if (Firebase.apps.isEmpty) {
|
||||
await Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
name: "sfm-notification"
|
||||
);
|
||||
AppLoggerUtils.warning("Firebase đã được khởi tạo trong background handler");
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
name: "sfm-notification");
|
||||
AppLoggerUtils.warning(
|
||||
"Firebase đã được khởi tạo trong background handler");
|
||||
} else {
|
||||
AppLoggerUtils.warning("Firebase đã được khởi tạo trước đó");
|
||||
}
|
||||
@@ -316,4 +328,4 @@ Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
||||
} catch (e) {
|
||||
AppLoggerUtils.warning("Error in background handler: $e");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user