123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- import 'dart:ui';
-
- import 'package:firebase_core/firebase_core.dart';
- import 'package:firebase_messaging/firebase_messaging.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_local_notifications/flutter_local_notifications.dart';
- import 'package:employee_selfservice_mobile/NotificationService/local_notification_service.dart';
- import 'package:employee_selfservice_mobile/Screens/Splash/splash_screen.dart';
- import 'Screens/Settings/settings_screen.dart';
- import 'constants.dart';
- import 'dart:developer' as logDev;
- import 'package:firebase_crashlytics/firebase_crashlytics.dart';
-
- Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
- logDev.log(message.data.toString(), name: "BACKGROUND HANDLER - DATANYA APA?");
- logDev.log(message.notification!.title.toString(), name: "BACKGROUND HANDLER - ISI NOTIFNYA APA?");
- }
-
- 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();
-
- void main() async{
- WidgetsFlutterBinding.ensureInitialized();
- await Firebase.initializeApp();
-
- //You can automatically catch all errors that are thrown within the Flutter framework by overriding
- FlutterError.onError = (errorDetails) {
- FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
- };
-
- // Pass all uncaught asynchronous errors that aren't handled by the Flutter framework to Crashlytics
- PlatformDispatcher.instance.onError = (error, stack) {
- FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
- return true;
- };
-
- //Force Crash - Crashlytics
- //FirebaseCrashlytics.instance.crash();
-
- //LocalNotificationService.initialize();
- FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
-
- await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
-
- LocalNotificationService.initialize();
-
- FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
- LocalNotificationService.initialize();
- 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}');
- }
-
- logDev.log(message.data.toString(), name: "on message listen - DATANYA APA?");
- logDev.log(message.notification!.title.toString(), name: "on message listen - ISI NOTIFNYA APA?");
- });
-
- await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
- alert: true,
- badge: true,
- sound: true,
- );
-
- runApp(MyApp());
- }
-
- class MyApp extends StatefulWidget {
- const MyApp({super.key});
-
- @override
- State<MyApp> createState() => _MyAppState();
- }
-
- class _MyAppState extends State<MyApp> {
- void initState(){
- super.initState();
-
- // 1. This method call when app in terminated state and you get a notification
- // when you click on notification app open from terminated state and you can get notification data in this method
- FirebaseMessaging.instance.getInitialMessage().then((message) {
- print("FirebaseMessaging.instance.getInitialMessage");
- if (message != null) {
- logDev.log("New Notification", name: "NEW NOTIF");
- // if (message.data['_id'] != null) {
- // Navigator.of(context).push(
- // MaterialPageRoute(
- // builder: (context) => DemoScreen(
- // id: message.data['_id'],
- // ),
- // ),
- // );
- // }
- }
- },
- );
-
- // 2. This method only call when App in forground it mean app must be opened
- FirebaseMessaging.onMessage.listen((RemoteMessage message) {
- RemoteNotification? notification = message.notification;
- //AndroidNotification? android = message.notification?.android;
- if (notification != null) {
- flutterLocalNotificationsPlugin.show(
- notification.hashCode,
- notification.title,
- notification.body,
- NotificationDetails(
- android: AndroidNotificationDetails(
- channel.id,
- channel.name,
- channelDescription: channel.description,
- color: Colors.blue,
- playSound: true,
- icon: '@mipmap/ic_launcher',
- ),
- ));
- }
- logDev.log(message.data.toString(), name: "onMessage listen - DATANYA APA?");
- logDev.log(message.notification!.title.toString(), name: "onMessage listen - ISI NOTIFNYA APA?");
- });
-
- // 3. This method only call when App in background and not terminated(not closed)
- FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
- print('A new onMessageOpenedApp event was published!');
- RemoteNotification? notification = message.notification;
- //AndroidNotification? android = message.notification?.android;
- if (notification != null) {
- showDialog(
- context: context,
- builder: (_) {
- return Scaffold(
- body: Builder(builder: (context) => Center(
- child: ElevatedButton(
- onPressed: () {
- Navigator.push(context, MaterialPageRoute(
- builder: (context) => SettingsScreen()));
- },
- child: Text("Click"),
- ),
- )),
- );
- });
- }
- logDev.log(message.data.toString(), name: "Opened APP - DATANYA APA?");
- logDev.log(message.notification!.title.toString(), name: "Opened APP - ISI NOTIFNYA APA?");
- });
- }
-
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- debugShowCheckedModeBanner: false,
- title: 'Flutter Welcome Screen',
- theme: ThemeData(
- primaryColor: kPrimaryColor,
- scaffoldBackgroundColor: Colors.white
- ),
- home: Splash(),
- );
- }
- }
-
- /*void sendLogMessage(String message) {
- FirebaseCrashlytics.instance.log(message);
- }*/
|