Advertisement
RestuRHP

PushNotificationService

Jul 2nd, 2024 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.04 KB | None | 0 0
  1. class FirebaseMessagingService {
  2.   static FirebaseMessaging? _firebaseMessaging;
  3.  
  4.   static FirebaseMessaging get firebaseMessaging => FirebaseMessagingService._firebaseMessaging ?? FirebaseMessaging.instance;
  5.  
  6.   final NotificationService notification = di.locator<NotificationService>();
  7.  
  8.   Future<void> initialize() async {
  9.     if (Platform.isIOS) {
  10.       NotificationSettings settings = await firebaseMessaging.requestPermission(
  11.         alert: true,
  12.         announcement: false,
  13.         badge: true,
  14.         carPlay: false,
  15.         criticalAlert: false,
  16.         provisional: false,
  17.         sound: true,
  18.       );
  19.  
  20.       if (settings.authorizationStatus == AuthorizationStatus.authorized) {
  21.         log('User granted permission');
  22.       } else if (settings.authorizationStatus == AuthorizationStatus.provisional) {
  23.         log('User granted provisional permission');
  24.       } else {
  25.         log('User declined or has not accepted permission');
  26.       }
  27.     }
  28.  
  29.     FirebaseMessaging.onMessage.listen((RemoteMessage message) {
  30.       if (message.data['source'] == 'Insider') {
  31.         FlutterInsider.Instance.handleNotification(<String, dynamic>{"data": message.data});
  32.       } else {
  33.         notification.showNotificationFirebase(message);
  34.       }
  35.       log("onMessages: ${message.data.toString()}");
  36.     });
  37.  
  38.     FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
  39.       if (message.data['source'] == 'Insider') {
  40.         FlutterInsider.Instance.handleNotification(<String, dynamic>{"data": message.data});
  41.       } else {
  42.         notification.showNotificationFirebase(message);
  43.       }
  44.       log("onLaunch: ${message.data}");
  45.     });
  46.  
  47.     FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  48.   }
  49.  
  50.   Future<void> onRefreshFcmToken(BuildContext context) async {
  51.     FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) async {
  52.       Provider.of<MemberProvider>(context, listen: false).updateFcmToken(token: fcmToken);
  53.       await Storage.save(deviceTokenStorageKey, fcmToken);
  54.     });
  55.   }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement