107 lines
4.0 KiB
Dart
107 lines
4.0 KiB
Dart
import 'package:didvan/models/notification_data.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';
|
||
import 'package:get/get.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");
|
||
}
|
||
}
|
||
} |