Sin descripción
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.4KB

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