import 'dart:convert'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/material.dart'; import 'package:test_sa/models/app_notification.dart'; import 'notification_manger.dart'; class FirebaseNotificationManger { static FirebaseMessaging messaging = FirebaseMessaging.instance; static String token; static Future getToken() async { NotificationSettings settings = await messaging.requestPermission( alert: true, announcement: false, badge: true, carPlay: false, criticalAlert: false, provisional: false, sound: true, ); if (settings.authorizationStatus == AuthorizationStatus.authorized) { token = await messaging.getToken(); } return token; } static initialized(BuildContext context) async { await Firebase.initializeApp(); NotificationSettings settings; try { settings = await messaging.requestPermission( alert: true, announcement: false, badge: true, carPlay: false, criticalAlert: false, provisional: false, sound: true, ); } catch (error) { return; } if (settings.authorizationStatus != AuthorizationStatus.authorized) { return; } // Also handle any interaction when the app is in the background via a // Stream listener FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler); FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { AppNotification notification = AppNotification.fromJson(message.data); if (notification.path == null || notification.path.isEmpty) return; Navigator.pushNamed(context, notification.path, arguments: notification.requestId); }); FirebaseMessaging.onMessage.listen((RemoteMessage message) { AppNotification notification = AppNotification.fromJson(message.data); NotificationManger.showNotification( title: message.notification.title, subtext: message.notification.body, hashcode: int.tryParse(notification.requestId ?? "") ?? 1, payload: json.encode(message.data)); return; }); } } Future firebaseMessagingBackgroundHandler(RemoteMessage message) async {}