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

2 months ago
  1. import 'dart:ui';
  2. import 'package:firebase_core/firebase_core.dart';
  3. import 'package:firebase_messaging/firebase_messaging.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_local_notifications/flutter_local_notifications.dart';
  6. import 'package:employee_selfservice_mobile/NotificationService/local_notification_service.dart';
  7. import 'package:employee_selfservice_mobile/Screens/Splash/splash_screen.dart';
  8. import 'Screens/Settings/settings_screen.dart';
  9. import 'constants.dart';
  10. import 'dart:developer' as logDev;
  11. import 'package:firebase_crashlytics/firebase_crashlytics.dart';
  12. import 'firebase_options.dart';
  13. const AndroidNotificationChannel channel = AndroidNotificationChannel(
  14. 'high_importance_channel', // id
  15. 'High Importance Notifications', // title
  16. description: 'This channel is used for important notifications.', // description
  17. importance: Importance.high,
  18. playSound: true);
  19. final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
  20. void main() async{
  21. WidgetsFlutterBinding.ensureInitialized();
  22. // if (Firebase.apps.length){
  23. await Firebase.initializeApp(
  24. name: "employee-self-service-8280b", options: DefaultFirebaseOptions.currentPlatform
  25. );
  26. // }
  27. //You can automatically catch all errors that are thrown within the Flutter framework by overriding
  28. FlutterError.onError = (errorDetails) {
  29. FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
  30. };
  31. // Pass all uncaught asynchronous errors that aren't handled by the Flutter framework to Crashlytics
  32. PlatformDispatcher.instance.onError = (error, stack) {
  33. FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
  34. return true;
  35. };
  36. //Force Crash - Crashlytics
  37. //FirebaseCrashlytics.instance.crash();
  38. //LocalNotificationService.initialize();
  39. FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  40. await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
  41. LocalNotificationService.initialize();
  42. FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
  43. LocalNotificationService.initialize();
  44. Map<String, dynamic> data = message.data;
  45. logDev.log(data.toString(), name: "NOTIF Messaging");
  46. String body = data['body'].toString();
  47. logDev.log(body, name: "NOTIF Body");
  48. print('Got a message whilst in the foreground!');
  49. print('Message data: ${message.data}');
  50. if (message.notification != null) {
  51. print('Message also contained a notification: ${message.notification}');
  52. }
  53. logDev.log(message.data.toString(), name: "on message listen - DATANYA APA?");
  54. logDev.log(message.notification!.title.toString(), name: "on message listen - ISI NOTIFNYA APA?");
  55. });
  56. await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
  57. alert: true,
  58. badge: true,
  59. sound: true,
  60. );
  61. runApp(MyApp());
  62. }
  63. class MyApp extends StatefulWidget {
  64. const MyApp({super.key});
  65. @override
  66. State<MyApp> createState() => _MyAppState();
  67. }
  68. class _MyAppState extends State<MyApp> {
  69. void initState(){
  70. super.initState();
  71. // 1. This method call when app in terminated state and you get a notification
  72. // when you click on notification app open from terminated state and you can get notification data in this method
  73. FirebaseMessaging.instance.getInitialMessage().then((message) {
  74. print("FirebaseMessaging.instance.getInitialMessage");
  75. if (message != null) {
  76. logDev.log("New Notification", name: "NEW NOTIF");
  77. // if (message.data['_id'] != null) {
  78. // Navigator.of(context).push(
  79. // MaterialPageRoute(
  80. // builder: (context) => DemoScreen(
  81. // id: message.data['_id'],
  82. // ),
  83. // ),
  84. // );
  85. // }
  86. }
  87. },
  88. );
  89. // 2. This method only call when App in forground it mean app must be opened
  90. FirebaseMessaging.onMessage.listen((RemoteMessage message) {
  91. RemoteNotification? notification = message.notification!;
  92. //AndroidNotification? android = message.notification?.android;
  93. if (notification != null) {
  94. flutterLocalNotificationsPlugin.show(
  95. notification.hashCode,
  96. notification.title,
  97. notification.body,
  98. NotificationDetails(
  99. android: AndroidNotificationDetails(
  100. channel.id,
  101. channel.name,
  102. channelDescription: channel.description,
  103. color: Colors.blue,
  104. playSound: true,
  105. icon: '@mipmap/ic_launcher',
  106. ),
  107. ));
  108. }
  109. logDev.log(message.data.toString(), name: "onMessage listen - DATANYA APA?");
  110. logDev.log(message.notification!.title.toString(), name: "onMessage listen - ISI NOTIFNYA APA?");
  111. });
  112. // 3. This method only call when App in background and not terminated(not closed)
  113. FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
  114. print('A new onMessageOpenedApp event was published!');
  115. RemoteNotification? notification = message.notification;
  116. //AndroidNotification? android = message.notification?.android;
  117. if (notification != null) {
  118. showDialog(
  119. context: context,
  120. builder: (_) {
  121. return Scaffold(
  122. body: Builder(builder: (context) => Center(
  123. child: ElevatedButton(
  124. onPressed: () {
  125. Navigator.push(context, MaterialPageRoute(
  126. builder: (context) => SettingsScreen()));
  127. },
  128. child: Text("Click"),
  129. ),
  130. )),
  131. );
  132. });
  133. }
  134. logDev.log(message.data.toString(), name: "Opened APP - DATANYA APA?");
  135. logDev.log(message.notification!.title.toString(), name: "Opened APP - ISI NOTIFNYA APA?");
  136. });
  137. }
  138. @override
  139. Widget build(BuildContext context) {
  140. return MaterialApp(
  141. debugShowCheckedModeBanner: false,
  142. title: 'Flutter Welcome Screen',
  143. theme: ThemeData(
  144. primaryColor: kPrimaryColor,
  145. scaffoldBackgroundColor: Colors.white
  146. ),
  147. home: Splash(),
  148. );
  149. }
  150. }
  151. Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  152. logDev.log(message.data.toString(), name: "BACKGROUND HANDLER - DATANYA APA?");
  153. logDev.log(message.notification!.title.toString(), name: "BACKGROUND HANDLER - ISI NOTIFNYA APA?");
  154. }
  155. /*void sendLogMessage(String message) {
  156. FirebaseCrashlytics.instance.log(message);
  157. }*/