103 lines
3.4 KiB
Dart
103 lines
3.4 KiB
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/notification/notification_service.dart';
|
|
import 'package:didvan/services/storage/storage.dart';
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
class FirebaseApi {
|
|
static String? fcmToken;
|
|
|
|
Future<void> initNotification() async {
|
|
if (kIsWeb) {
|
|
try {
|
|
bool isSupported = await FirebaseMessaging.instance.isSupported();
|
|
if (!isSupported) {
|
|
if (kDebugMode) {
|
|
print(
|
|
"⚠️ مرورگر از نوتیفیکیشن پشتیبانی نمیکند. پروسه نوتیفیکیشن با موفقیت لغو شد تا از کرش جلوگیری شود.");
|
|
}
|
|
return;
|
|
}
|
|
} catch (e) {
|
|
if (kDebugMode) print("Error checking FCM support: $e");
|
|
return;
|
|
}
|
|
}
|
|
|
|
try {
|
|
final messaging = FirebaseMessaging.instance;
|
|
|
|
NotificationSettings settings = await messaging.requestPermission(
|
|
alert: true,
|
|
announcement: true,
|
|
badge: true,
|
|
carPlay: false,
|
|
criticalAlert: true,
|
|
provisional: true,
|
|
sound: true,
|
|
);
|
|
|
|
if (settings.authorizationStatus == AuthorizationStatus.authorized ||
|
|
settings.authorizationStatus == AuthorizationStatus.provisional) {
|
|
if (kIsWeb) {
|
|
fcmToken = await messaging.getToken(
|
|
vapidKey:
|
|
"BMXHGd93t_htpS7c62ceuuLVVmia2cEDmqxp46g9Vt0B3OxNMKIqN9nupsUMtv2Vq8Yy2sQGIqgCm9FxUSKvssU",
|
|
);
|
|
} else {
|
|
fcmToken = await messaging.getToken();
|
|
}
|
|
|
|
if (kDebugMode) {
|
|
print("fCMToken: $fcmToken");
|
|
}
|
|
} else {
|
|
if (kDebugMode) {
|
|
print("User declined or has not accepted notification permissions");
|
|
}
|
|
}
|
|
} catch (e) {
|
|
if (kDebugMode) print("Error initializing notifications: $e");
|
|
}
|
|
|
|
try {
|
|
final initMsg = await FirebaseMessaging.instance.getInitialMessage();
|
|
if (initMsg != null) {
|
|
if (kDebugMode)
|
|
print("=== APP OPENED FROM NOTIFICATION (TERMINATED) ===");
|
|
NotificationMessage data = NotificationMessage.fromJson(initMsg.data);
|
|
HomeWidgetRepository.data = data;
|
|
}
|
|
} catch (e) {
|
|
if (kDebugMode) print("Error handling initial message: $e");
|
|
}
|
|
|
|
FirebaseMessaging.onMessageOpenedApp.listen((initMsg) async {
|
|
try {
|
|
NotificationMessage data = NotificationMessage.fromJson(initMsg.data);
|
|
HomeWidgetRepository.data = data;
|
|
await Future.delayed(const Duration(milliseconds: 300));
|
|
await HomeWidgetRepository.decideWhereToGoNotif();
|
|
await StorageService.delete(
|
|
key: 'notification${AppInitializer.createNotificationId(data)}');
|
|
} catch (e) {
|
|
if (kDebugMode) print("Error handling background message: $e");
|
|
}
|
|
});
|
|
|
|
FirebaseMessaging.onMessage.listen((event) => handleMessage(event));
|
|
}
|
|
|
|
void handleMessage(RemoteMessage? message) async {
|
|
if (message == null) return;
|
|
|
|
try {
|
|
await NotificationService.showFirebaseNotification(message);
|
|
} catch (e) {
|
|
if (kDebugMode) print("Error showing local notification: $e");
|
|
}
|
|
}
|
|
}
|