Update: 2026-05-06 04:55:22
This commit is contained in:
34
musadaq-app/lib/core/services/push_notification_service.dart
Normal file
34
musadaq-app/lib/core/services/push_notification_service.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import '../utils/logger.dart';
|
||||
|
||||
class PushNotificationService {
|
||||
static final FirebaseMessaging _fcm = FirebaseMessaging.instance;
|
||||
|
||||
static Future<void> initialize() async {
|
||||
// 1. Request permissions (iOS)
|
||||
NotificationSettings settings = await _fcm.requestPermission(
|
||||
alert: true,
|
||||
badge: true,
|
||||
sound: true,
|
||||
);
|
||||
|
||||
AppLogger.print('User granted permission: ${settings.authorizationStatus}');
|
||||
|
||||
// 2. Handle foreground messages
|
||||
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
|
||||
AppLogger.print('Received foreground message: ${message.notification?.title}');
|
||||
// You can show a local notification here if needed
|
||||
});
|
||||
}
|
||||
|
||||
static Future<String?> getToken() async {
|
||||
try {
|
||||
String? token = await _fcm.getToken();
|
||||
AppLogger.print('FCM Token: $token');
|
||||
return token;
|
||||
} catch (e) {
|
||||
AppLogger.error('Failed to get FCM token', e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user