GSI - Employe Self Service Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

182 lines
6.6 KiB

import 'dart:ui';
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:employee_selfservice_mobile/NotificationService/local_notification_service.dart';
import 'package:employee_selfservice_mobile/Screens/Splash/splash_screen.dart';
import 'Screens/Settings/settings_screen.dart';
import 'constants.dart';
import 'dart:developer' as logDev;
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'firebase_options.dart';
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();
void main() async{
WidgetsFlutterBinding.ensureInitialized();
// if (Firebase.apps.length){
await Firebase.initializeApp(
name: "employee-self-service-8280b", options: DefaultFirebaseOptions.currentPlatform
);
// }
//You can automatically catch all errors that are thrown within the Flutter framework by overriding
FlutterError.onError = (errorDetails) {
FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
};
// Pass all uncaught asynchronous errors that aren't handled by the Flutter framework to Crashlytics
PlatformDispatcher.instance.onError = (error, stack) {
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
return true;
};
//Force Crash - Crashlytics
//FirebaseCrashlytics.instance.crash();
//LocalNotificationService.initialize();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
LocalNotificationService.initialize();
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
LocalNotificationService.initialize();
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();
// 1. This method call when app in terminated state and you get a notification
// when you click on notification app open from terminated state and you can get notification data in this method
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'],
// ),
// ),
// );
// }
}
},
);
// 2. This method only call when App in forground it mean app must be opened
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?");
});
// 3. This method only call when App in background and not terminated(not closed)
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('A new onMessageOpenedApp event was published!');
RemoteNotification? notification = message.notification;
//AndroidNotification? android = message.notification?.android;
if (notification != null) {
showDialog(
context: context,
builder: (_) {
return Scaffold(
body: Builder(builder: (context) => Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) => SettingsScreen()));
},
child: Text("Click"),
),
)),
);
});
}
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(),
);
}
}
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 sendLogMessage(String message) {
FirebaseCrashlytics.instance.log(message);
}*/