didvan-app/lib/services/notification/fcm/firebase_notification_handl...

162 lines
6.0 KiB
Dart

import 'package:didvan/services/notification/awsome/awsome_notification_handler.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import '../lc/local_notification_service.dart';
import '../../app_initalizer.dart';
class FirebaseNotificationHandler {
Future<void> initial() async {
try {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: kIsWeb
? const FirebaseOptions(
apiKey: "AIzaSyA0HZjKpRuPOi1SC3f_EZTvlS3mcj9UVo0",
authDomain: "didvan-9b7da.firebaseapp.com",
projectId: "didvan-9b7da",
storageBucket: "didvan-9b7da.appspot.com",
messagingSenderId: "935017686266",
appId: "1:935017686266:web:a93f7a19bed23c51d2d543",
measurementId: "G-80B4H9E8Y0")
: const FirebaseOptions(
apiKey: 'AIzaSyBp-UHjWeM0H0UHtX5yguFKG-riMzvvCzw',
appId: '1:935017686266:android:f9cbc9aba8e3d65ed2d543',
messagingSenderId: '935017686266',
projectId: 'didvan-9b7da',
),
);
final initMsg = await FirebaseMessaging.instance.getInitialMessage();
if (initMsg != null) {
AppInitializer.clickAction = initMsg.data['click_action'].replaceAll(
'navigate-',
'',
);
}
// LocalNotificationService.initialize();
NotificationSettings settings =
await FirebaseMessaging.instance.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
debugPrint(
'User granted notifications permission: ${settings.authorizationStatus}');
// Handling background messages using the specified handler
// FirebaseMessaging.onBackgroundMessage(
// _firebaseMessagingBackgroundHandler);
// Listening for incoming messages while the app is in the foreground
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
if (message.data != null) {
/* if (message.notification!.title != null &&
message.notification!.body != null) {
final notificationData = message.data;
final screen = notificationData['screen'];
// Showing an alert dialog when a notification is received (Foreground state)
// showDialog(
// context: context,
// barrierDismissible: false,
// builder: (BuildContext context) {
// return WillPopScope(
// onWillPop: () async => false,
// child: AlertDialog(
// title: Text(message.notification!.title!),
// content: Text(message.notification!.body!),
// actions: [
// if (notificationData.containsKey('screen'))
// TextButton(
// onPressed: () {
// Navigator.pop(context);
// Navigator.of(context).pushNamed(screen);
// },
// child: const Text('Open Screen'),
// ),
// TextButton(
// onPressed: () => Navigator.of(context).pop(),
// child: const Text('Dismiss'),
// ),
// ],
// ),
// );
// },
// );
}*/
// LocalNotificationService.initialize();
// LocalNotificationService.showBigPictureNotification();
// LocalNotificationService.display(message);
AwsomeNotificationHandler().main();
AwsomeNotificationHandler().show(message);
}
});
// Handling the initial message received when the app is launched from dead (killed state)
// When the app is killed and a new notification arrives when user clicks on it
// It gets the data to which screen to open
FirebaseMessaging.instance.getInitialMessage().then((message) {
if (message != null) {
_handleNotificationClick(message);
}
});
// Handling a notification click event when the app is in the background
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
_handleNotificationClick(message);
});
} catch (e) {
print(e.toString());
}
}
// Handling a notification click event by navigating to the specified screen
void _handleNotificationClick(RemoteMessage message) {
final notificationData = message.data;
if (notificationData.containsKey('screen')) {
final screen = notificationData['screen'];
// Navigator.of(context).pushNamed(screen);
}
}
}
// Handler for background messages
// @pragma('vm:entry-point')
// Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// await Firebase.initializeApp(
// options: kIsWeb
// ? const FirebaseOptions(
// apiKey: "AIzaSyA0HZjKpRuPOi1SC3f_EZTvlS3mcj9UVo0",
// authDomain: "didvan-9b7da.firebaseapp.com",
// projectId: "didvan-9b7da",
// storageBucket: "didvan-9b7da.appspot.com",
// messagingSenderId: "935017686266",
// appId: "1:935017686266:web:a93f7a19bed23c51d2d543",
// measurementId: "G-80B4H9E8Y0")
// : const FirebaseOptions(
// apiKey: 'AIzaSyBp-UHjWeM0H0UHtX5yguFKG-riMzvvCzw',
// appId: '1:935017686266:android:f9cbc9aba8e3d65ed2d543',
// messagingSenderId: '935017686266',
// projectId: 'didvan-9b7da',
// ),
// );
//
// LocalNotificationService.initialize();
// LocalNotificationService.display(message);
// // AwsomeNotificationHandler().main();
// // AwsomeNotificationHandler().show(message);
//
// return Future<void>.value();
// }