Browse Source

Update 24/03

dienianindya 1 year ago
parent
commit
50a9c53192

+ 3
- 0
android/app/build.gradle View File

21
     flutterVersionName = '1.0'
21
     flutterVersionName = '1.0'
22
 }
22
 }
23
 
23
 
24
+apply plugin: 'com.google.gms.google-services'
24
 apply plugin: 'com.android.application'
25
 apply plugin: 'com.android.application'
25
 apply plugin: 'kotlin-android'
26
 apply plugin: 'kotlin-android'
26
 apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
27
 apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
64
 
65
 
65
 dependencies{
66
 dependencies{
66
     implementation 'com.android.support:multidex:1.0.3'
67
     implementation 'com.android.support:multidex:1.0.3'
68
+    implementation platform('com.google.firebase:firebase-bom:30.2.0')
69
+    implementation 'com.google.firebase:firebase-analytics'
67
 }
70
 }

+ 6
- 1
android/app/src/main/AndroidManifest.xml View File

15
         <meta-data android:name="com.google.android.geo.API_KEY"
15
         <meta-data android:name="com.google.android.geo.API_KEY"
16
             android:value="YOUR-KEY-HERE"/>
16
             android:value="YOUR-KEY-HERE"/>
17
 
17
 
18
-
19
         <activity
18
         <activity
20
             android:name=".MainActivity"
19
             android:name=".MainActivity"
20
+            android:showWhenLocked="true"
21
+            android:turnScreenOn="true"
21
             android:screenOrientation="portrait"
22
             android:screenOrientation="portrait"
22
             android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
23
             android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
23
             android:exported="true"
24
             android:exported="true"
47
         <meta-data
48
         <meta-data
48
             android:name="flutterEmbedding"
49
             android:name="flutterEmbedding"
49
             android:value="2" />
50
             android:value="2" />
51
+
52
+        <meta-data
53
+            android:name="com.google.firebase.messaging.default_notification_channel_id"
54
+            android:value="high_importance_channel" />
50
     </application>
55
     </application>
51
 </manifest>
56
 </manifest>
52
 
57
 

+ 2
- 0
android/build.gradle View File

8
     dependencies {
8
     dependencies {
9
         classpath 'com.android.tools.build:gradle:7.1.3'
9
         classpath 'com.android.tools.build:gradle:7.1.3'
10
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
10
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
11
+        classpath 'com.google.gms:google-services:4.3.15'
12
+
11
     }
13
     }
12
 }
14
 }
13
 
15
 

+ 50
- 0
lib/NotificationService/local_notification_service.dart View File

1
+import 'package:firebase_messaging/firebase_messaging.dart';
2
+import 'package:flutter/material.dart';
3
+import 'package:flutter_local_notifications/flutter_local_notifications.dart';
4
+
5
+import '../Screens/Settings/settings_screen.dart';
6
+
7
+class LocalNotificationService{
8
+  static final FlutterLocalNotificationsPlugin _notificationsPlugin = FlutterLocalNotificationsPlugin();
9
+
10
+  static void initialize() {
11
+    // initializationSettings  for Android
12
+    const InitializationSettings initializationSettings = InitializationSettings(
13
+      android: AndroidInitializationSettings("@mipmap/ic_launcher"),
14
+    );
15
+
16
+    _notificationsPlugin.initialize(initializationSettings, onDidReceiveNotificationResponse:  (details) {
17
+      print("onSelectNotification");
18
+        var context;
19
+        Navigator.push(context as BuildContext, MaterialPageRoute(
20
+                builder: (context) => SettingsScreen()));
21
+
22
+      },
23
+    );
24
+  }
25
+
26
+  static void createanddisplaynotification(RemoteMessage message) async {
27
+    try {
28
+      final id = DateTime.now().millisecondsSinceEpoch ~/ 1000;
29
+      const NotificationDetails notificationDetails = NotificationDetails(
30
+        android: AndroidNotificationDetails(
31
+          "pushnotificationapp",
32
+          "pushnotificationappchannel",
33
+          icon: "@mipmap/ic_launcher",
34
+          importance: Importance.max,
35
+          priority: Priority.high,
36
+        ),
37
+      );
38
+
39
+      await _notificationsPlugin.show(
40
+        id,
41
+        message.notification!.title,
42
+        message.notification!.body,
43
+        notificationDetails,
44
+        payload: message.data['_id'],
45
+      );
46
+    } on Exception catch (e) {
47
+      print(e);
48
+    }
49
+  }
50
+}

+ 85
- 44
lib/main.dart View File

1
 import 'package:firebase_core/firebase_core.dart';
1
 import 'package:firebase_core/firebase_core.dart';
2
 import 'package:firebase_messaging/firebase_messaging.dart';
2
 import 'package:firebase_messaging/firebase_messaging.dart';
3
-import 'package:flutter/foundation.dart';
4
 import 'package:flutter/material.dart';
3
 import 'package:flutter/material.dart';
5
-import 'package:flutter/services.dart';
6
 import 'package:flutter_local_notifications/flutter_local_notifications.dart';
4
 import 'package:flutter_local_notifications/flutter_local_notifications.dart';
5
+import 'package:hris_selfservice_mobile/NotificationService/local_notification_service.dart';
7
 import 'package:hris_selfservice_mobile/Screens/Splash/splash_screen.dart';
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
 import 'constants.dart';
8
 import 'constants.dart';
10
-import 'firebase_options.dart';
11
 import 'dart:developer' as logDev;
9
 import 'dart:developer' as logDev;
12
 
10
 
13
 const AndroidNotificationChannel channel = AndroidNotificationChannel(
11
 const AndroidNotificationChannel channel = AndroidNotificationChannel(
14
     'high_importance_channel', // id
12
     'high_importance_channel', // id
15
     'High Importance Notifications', // title
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
     importance: Importance.high,
15
     importance: Importance.high,
19
     playSound: true);
16
     playSound: true);
20
 
17
 
21
 final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
18
 final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
22
 
19
 
23
 Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
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
 void main() async{
25
 void main() async{
38
   WidgetsFlutterBinding.ensureInitialized();
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
   FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
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
   FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
33
   FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
60
     Map<String, dynamic> data = message.data;
34
     Map<String, dynamic> data = message.data;
69
     if (message.notification != null) {
43
     if (message.notification != null) {
70
       print('Message also contained a notification: ${message.notification}');
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
   await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
51
   await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
84
     alert: true,
52
     alert: true,
89
   runApp(MyApp());
57
   runApp(MyApp());
90
 }
58
 }
91
 
59
 
92
-class MyApp extends StatelessWidget {
60
+class MyApp extends StatefulWidget {
93
   const MyApp({super.key});
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
   @override
136
   @override
96
   Widget build(BuildContext context) {
137
   Widget build(BuildContext context) {
97
     return MaterialApp(
138
     return MaterialApp(

Loading…
Cancel
Save