Brak opisu
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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import 'package:firebase_core/firebase_core.dart';
  2. import 'package:firebase_messaging/firebase_messaging.dart';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:flutter_local_notifications/flutter_local_notifications.dart';
  7. import 'package:hris_selfservice_mobile/Screens/Splash/splash_screen.dart';
  8. //import 'package:hris_selfservice_mobile/Screens/ForgotPassword/forgotPassword_screen.dart';
  9. import 'constants.dart';
  10. import 'firebase_options.dart';
  11. import 'dart:developer' as logDev;
  12. const AndroidNotificationChannel channel = AndroidNotificationChannel(
  13. 'high_importance_channel', // id
  14. 'High Importance Notifications', // title
  15. description:
  16. 'This channel is used for important notifications.', // description
  17. importance: Importance.high,
  18. playSound: true);
  19. final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
  20. Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  21. // If you're going to use other Firebase services in the background, such as Firestore,
  22. // make sure you call `initializeApp` before using other Firebase services.
  23. await Firebase.initializeApp();
  24. Map<String, dynamic> data = message.data;
  25. logDev.log(data.toString(), name: "NOTIF Messaging");
  26. String body = data['body'].toString();
  27. logDev.log(body, name: "NOTIF Body");
  28. print("Handling a background message: ${message.messageId}");
  29. }
  30. void main() async{
  31. WidgetsFlutterBinding.ensureInitialized();
  32. await Firebase.initializeApp(
  33. options: DefaultFirebaseOptions.currentPlatform,
  34. );
  35. FirebaseMessaging messaging = FirebaseMessaging.instance;
  36. FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  37. NotificationSettings settings = await messaging.requestPermission(
  38. alert: true,
  39. announcement: false,
  40. badge: true,
  41. carPlay: false,
  42. criticalAlert: false,
  43. provisional: false,
  44. sound: true,
  45. );
  46. print('User granted permission: ${settings.authorizationStatus}');
  47. FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
  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. });
  58. SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
  59. statusBarColor: Colors.transparent,
  60. ));
  61. FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  62. await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
  63. await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
  64. alert: true,
  65. badge: true,
  66. sound: true,
  67. );
  68. runApp(MyApp());
  69. }
  70. class MyApp extends StatelessWidget {
  71. const MyApp({super.key});
  72. @override
  73. Widget build(BuildContext context) {
  74. return MaterialApp(
  75. debugShowCheckedModeBanner: false,
  76. title: 'Flutter Welcome Screen',
  77. theme: ThemeData(
  78. primaryColor: kPrimaryColor,
  79. scaffoldBackgroundColor: Colors.white
  80. ),
  81. home: Splash(),
  82. );
  83. }
  84. }