Bez popisu
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.

main.dart 6.6KB

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