123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- 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<void> _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<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
-
- FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
- Map<String, dynamic> 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<MyApp> createState() => _MyAppState();
- }
-
- class _MyAppState extends State<MyApp> {
- 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(),
- );
- }
- }
|