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