Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

main.dart 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import 'package:firebase_core/firebase_core.dart';
  2. import 'package:firebase_messaging/firebase_messaging.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_local_notifications/flutter_local_notifications.dart';
  5. import 'package:hris_selfservice_mobile/NotificationService/local_notification_service.dart';
  6. import 'package:hris_selfservice_mobile/Screens/Splash/splash_screen.dart';
  7. import 'Screens/Settings/settings_screen.dart';
  8. import 'constants.dart';
  9. import 'dart:developer' as logDev;
  10. const AndroidNotificationChannel channel = AndroidNotificationChannel(
  11. 'high_importance_channel', // id
  12. 'High Importance Notifications', // title
  13. description: 'This channel is used for important notifications.', // description
  14. importance: Importance.high,
  15. playSound: true);
  16. final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
  17. Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  18. logDev.log(message.data.toString(), name: "BACKGROUND HANDLER - DATANYA APA?");
  19. logDev.log(message.notification!.title.toString(), name: "BACKGROUND HANDLER - ISI NOTIFNYA APA?");
  20. }
  21. void main() async{
  22. WidgetsFlutterBinding.ensureInitialized();
  23. await Firebase.initializeApp();
  24. LocalNotificationService.initialize();
  25. FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  26. await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
  27. FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
  28. Map<String, dynamic> data = message.data;
  29. logDev.log(data.toString(), name: "NOTIF Messaging");
  30. String body = data['body'].toString();
  31. logDev.log(body, name: "NOTIF Body");
  32. print('Got a message whilst in the foreground!');
  33. print('Message data: ${message.data}');
  34. if (message.notification != null) {
  35. print('Message also contained a notification: ${message.notification}');
  36. }
  37. logDev.log(message.data.toString(), name: "on message listen - DATANYA APA?");
  38. logDev.log(message.notification!.title.toString(), name: "on message listen - ISI NOTIFNYA APA?");
  39. });
  40. await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
  41. alert: true,
  42. badge: true,
  43. sound: true,
  44. );
  45. runApp(MyApp());
  46. }
  47. class MyApp extends StatefulWidget {
  48. const MyApp({super.key});
  49. @override
  50. State<MyApp> createState() => _MyAppState();
  51. }
  52. class _MyAppState extends State<MyApp> {
  53. void initState(){
  54. super.initState();
  55. FirebaseMessaging.instance.getInitialMessage().then((message) {
  56. print("FirebaseMessaging.instance.getInitialMessage");
  57. if (message != null) {
  58. logDev.log("New Notification", name: "NEW NOTIF");
  59. // if (message.data['_id'] != null) {
  60. // Navigator.of(context).push(
  61. // MaterialPageRoute(
  62. // builder: (context) => DemoScreen(
  63. // id: message.data['_id'],
  64. // ),
  65. // ),
  66. // );
  67. // }
  68. }
  69. },
  70. );
  71. FirebaseMessaging.onMessage.listen((RemoteMessage message) {
  72. RemoteNotification? notification = message.notification;
  73. //AndroidNotification? android = message.notification?.android;
  74. if (notification != null) {
  75. flutterLocalNotificationsPlugin.show(
  76. notification.hashCode,
  77. notification.title,
  78. notification.body,
  79. NotificationDetails(
  80. android: AndroidNotificationDetails(
  81. channel.id,
  82. channel.name,
  83. channelDescription: channel.description,
  84. color: Colors.blue,
  85. playSound: true,
  86. icon: '@mipmap/ic_launcher',
  87. ),
  88. ));
  89. }
  90. logDev.log(message.data.toString(), name: "onMessage listen - DATANYA APA?");
  91. logDev.log(message.notification!.title.toString(), name: "onMessage listen - ISI NOTIFNYA APA?");
  92. });
  93. FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
  94. print('A new onMessageOpenedApp event was published!');
  95. RemoteNotification? notification = message.notification;
  96. AndroidNotification? android = message.notification?.android;
  97. await Navigator.push(context as BuildContext, MaterialPageRoute(builder: (context) => SettingsScreen()));
  98. if (notification != null) {
  99. showDialog(
  100. context: context,
  101. builder: (_) {
  102. return AlertDialog(
  103. title: Text(notification.title!),
  104. content: SingleChildScrollView(
  105. child: Column(
  106. crossAxisAlignment: CrossAxisAlignment.start,
  107. children: [Text(notification.body!)],
  108. ),
  109. ),
  110. );
  111. });
  112. }
  113. logDev.log(message.data.toString(), name: "Opened APP - DATANYA APA?");
  114. logDev.log(message.notification!.title.toString(), name: "Opened APP - ISI NOTIFNYA APA?");
  115. });
  116. }
  117. @override
  118. Widget build(BuildContext context) {
  119. return MaterialApp(
  120. debugShowCheckedModeBanner: false,
  121. title: 'Flutter Welcome Screen',
  122. theme: ThemeData(
  123. primaryColor: kPrimaryColor,
  124. scaffoldBackgroundColor: Colors.white
  125. ),
  126. home: Splash(),
  127. );
  128. }
  129. }