123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import 'package:firebase_core/firebase_core.dart';
- import 'package:firebase_messaging/firebase_messaging.dart';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:flutter_local_notifications/flutter_local_notifications.dart';
- import 'package:hris_selfservice_mobile/Screens/Splash/splash_screen.dart';
- //import 'package:hris_selfservice_mobile/Screens/ForgotPassword/forgotPassword_screen.dart';
- import 'constants.dart';
- import 'firebase_options.dart';
- import 'dart:developer' as logDev;
-
- const AndroidNotificationChannel channel = AndroidNotificationChannel(
- 'high_importance_channel', // id
- 'High Importance Notifications', // title
- description:
- 'This channel is used for important notifications.', // description
- importance: Importance.high,
- playSound: true);
-
- final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
-
- Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
- // If you're going to use other Firebase services in the background, such as Firestore,
- // make sure you call `initializeApp` before using other Firebase services.
- await Firebase.initializeApp();
-
- Map<String, dynamic> data = message.data;
- logDev.log(data.toString(), name: "NOTIF Messaging");
-
- String body = data['body'].toString();
- logDev.log(body, name: "NOTIF Body");
-
- print("Handling a background message: ${message.messageId}");
- }
-
- void main() async{
- WidgetsFlutterBinding.ensureInitialized();
- await Firebase.initializeApp(
- options: DefaultFirebaseOptions.currentPlatform,
- );
-
- FirebaseMessaging messaging = FirebaseMessaging.instance;
- FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
-
-
- NotificationSettings settings = await messaging.requestPermission(
- alert: true,
- announcement: false,
- badge: true,
- carPlay: false,
- criticalAlert: false,
- provisional: false,
- sound: true,
- );
-
- print('User granted permission: ${settings.authorizationStatus}');
-
- FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
- Map<String, dynamic> data = message.data;
- logDev.log(data.toString(), name: "NOTIF Messaging");
-
- String body = data['body'].toString();
- logDev.log(body, name: "NOTIF Body");
-
- print('Got a message whilst in the foreground!');
- print('Message data: ${message.data}');
-
- if (message.notification != null) {
- print('Message also contained a notification: ${message.notification}');
- }
- });
-
-
- SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
- statusBarColor: Colors.transparent,
- ));
-
- FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
-
- await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
-
- await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
- alert: true,
- badge: true,
- sound: true,
- );
-
- runApp(MyApp());
- }
-
- class MyApp extends StatelessWidget {
- const MyApp({super.key});
-
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- debugShowCheckedModeBanner: false,
- title: 'Flutter Welcome Screen',
- theme: ThemeData(
- primaryColor: kPrimaryColor,
- scaffoldBackgroundColor: Colors.white
- ),
- home: Splash(),
- );
- }
- }
|