import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/material.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:hris_selfservice_mobile/NotificationService/local_notification_service.dart'; import 'package:hris_selfservice_mobile/Screens/Splash/splash_screen.dart'; import 'Screens/Settings/settings_screen.dart'; import 'constants.dart'; import 'dart:developer' as logDev; const AndroidNotificationChannel channel = AndroidNotificationChannel( 'high_importance_channel', // id 'High Importance Notifications', // title description: 'This channel is used for important notifications.', // description importance: Importance.high, playSound: true); final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { logDev.log(message.data.toString(), name: "BACKGROUND HANDLER - DATANYA APA?"); logDev.log(message.notification!.title.toString(), name: "BACKGROUND HANDLER - ISI NOTIFNYA APA?"); } void main() async{ WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); LocalNotificationService.initialize(); FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation()?.createNotificationChannel(channel); FirebaseMessaging.onMessage.listen((RemoteMessage message) async { Map data = message.data; logDev.log(data.toString(), name: "NOTIF Messaging"); String body = data['body'].toString(); logDev.log(body, name: "NOTIF Body"); print('Got a message whilst in the foreground!'); print('Message data: ${message.data}'); if (message.notification != null) { print('Message also contained a notification: ${message.notification}'); } logDev.log(message.data.toString(), name: "on message listen - DATANYA APA?"); logDev.log(message.notification!.title.toString(), name: "on message listen - ISI NOTIFNYA APA?"); }); await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions( alert: true, badge: true, sound: true, ); runApp(MyApp()); } class MyApp extends StatefulWidget { const MyApp({super.key}); @override State createState() => _MyAppState(); } class _MyAppState extends State { void initState(){ super.initState(); FirebaseMessaging.instance.getInitialMessage().then((message) { print("FirebaseMessaging.instance.getInitialMessage"); if (message != null) { logDev.log("New Notification", name: "NEW NOTIF"); // if (message.data['_id'] != null) { // Navigator.of(context).push( // MaterialPageRoute( // builder: (context) => DemoScreen( // id: message.data['_id'], // ), // ), // ); // } } }, ); FirebaseMessaging.onMessage.listen((RemoteMessage message) { RemoteNotification? notification = message.notification; //AndroidNotification? android = message.notification?.android; if (notification != null) { flutterLocalNotificationsPlugin.show( notification.hashCode, notification.title, notification.body, NotificationDetails( android: AndroidNotificationDetails( channel.id, channel.name, channelDescription: channel.description, color: Colors.blue, playSound: true, icon: '@mipmap/ic_launcher', ), )); } logDev.log(message.data.toString(), name: "onMessage listen - DATANYA APA?"); logDev.log(message.notification!.title.toString(), name: "onMessage listen - ISI NOTIFNYA APA?"); }); FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async { print('A new onMessageOpenedApp event was published!'); RemoteNotification? notification = message.notification; AndroidNotification? android = message.notification?.android; await Navigator.push(context as BuildContext, MaterialPageRoute(builder: (context) => SettingsScreen())); if (notification != null) { showDialog( context: context, builder: (_) { return AlertDialog( title: Text(notification.title!), content: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [Text(notification.body!)], ), ), ); }); } logDev.log(message.data.toString(), name: "Opened APP - DATANYA APA?"); logDev.log(message.notification!.title.toString(), name: "Opened APP - ISI NOTIFNYA APA?"); }); } @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter Welcome Screen', theme: ThemeData( primaryColor: kPrimaryColor, scaffoldBackgroundColor: Colors.white ), home: Splash(), ); } }