-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a677b38
commit 012e6fb
Showing
15 changed files
with
496 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"project_info": { | ||
"project_number": "788037853247", | ||
"project_id": "attendance-338eb", | ||
"storage_bucket": "attendance-338eb.appspot.com" | ||
}, | ||
"client": [ | ||
{ | ||
"client_info": { | ||
"mobilesdk_app_id": "1:788037853247:android:c1e473148d4ba293e7988a", | ||
"android_client_info": { | ||
"package_name": "com.pclub.attendance" | ||
} | ||
}, | ||
"oauth_client": [], | ||
"api_key": [ | ||
{ | ||
"current_key": "AIzaSyATmGPGCnhEzncmC9lYhixCTn0jvXrhykA" | ||
} | ||
], | ||
"services": { | ||
"appinvite_service": { | ||
"other_platform_oauth_client": [] | ||
} | ||
} | ||
} | ||
], | ||
"configuration_version": "1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...om/example/attendance_app/MainActivity.kt → ...tlin/com/pclub/attendance/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"flutter":{"platforms":{"android":{"default":{"projectId":"attendance-338eb","appId":"1:788037853247:android:c1e473148d4ba293e7988a","fileOutput":"android/app/google-services.json"}},"dart":{"lib/firebase_options.dart":{"projectId":"attendance-338eb","configurations":{"android":"1:788037853247:android:c1e473148d4ba293e7988a"}}}}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'package:firebase_core/firebase_core.dart'; | ||
import 'package:firebase_messaging/firebase_messaging.dart'; | ||
import 'storage.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:hive/hive.dart'; | ||
import 'package:permission_handler/permission_handler.dart'; | ||
import 'localStorage.dart'; | ||
import 'notification.dart'; | ||
import 'firebase_options.dart'; | ||
|
||
class FirebaseConfig { | ||
static Future<void> initialize() async { | ||
await Firebase.initializeApp( | ||
options: DefaultFirebaseOptions.currentPlatform | ||
); | ||
} | ||
|
||
static void listenNotification() { | ||
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { | ||
}); | ||
} | ||
|
||
static Future<void> checkForInitialMessage() async { | ||
RemoteMessage? initialMessage = | ||
await FirebaseMessaging.instance.getInitialMessage(); | ||
} | ||
|
||
static Future<PermissionStatus> _getNotificationsPermission() async { | ||
var status = await Permission.notification.status; | ||
if (!status.isGranted) { | ||
final result = await Permission.notification.request(); | ||
return result; | ||
} else { | ||
return status; | ||
} | ||
} | ||
|
||
static Future<void> registerNotification() async { | ||
PermissionStatus status=await _getNotificationsPermission(); | ||
if(kDebugMode){ | ||
print(status); | ||
} | ||
var messaging = FirebaseMessaging.instance; | ||
print("FCM TOKEN: ${await messaging.getToken()}"); | ||
NotificationSettings settings = await messaging.requestPermission( | ||
alert: true, | ||
announcement: false, | ||
badge: true, | ||
carPlay: false, | ||
criticalAlert: false, | ||
provisional: false, | ||
sound: true, | ||
); | ||
if (settings.authorizationStatus == AuthorizationStatus.authorized) { | ||
FirebaseMessaging.onMessage.listen((RemoteMessage message) { | ||
LocalStorage.addNotification(message.notification!.toMap()); | ||
NotificationConfig.showNotification(message); | ||
}); | ||
} | ||
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); | ||
} | ||
@pragma('vm:entry-point') | ||
static Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { | ||
Firebase.initializeApp(); | ||
await Storage.initialize(); | ||
if(Hive.box("localStorage").isOpen){ | ||
LocalStorage.addNotification(message.notification!.toMap()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// File generated by FlutterFire CLI. | ||
// ignore_for_file: type=lint | ||
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions; | ||
import 'package:flutter/foundation.dart' | ||
show defaultTargetPlatform, kIsWeb, TargetPlatform; | ||
|
||
/// Default [FirebaseOptions] for use with your Firebase apps. | ||
/// | ||
/// Example: | ||
/// ```dart | ||
/// import 'firebase_options.dart'; | ||
/// // ... | ||
/// await Firebase.initializeApp( | ||
/// options: DefaultFirebaseOptions.currentPlatform, | ||
/// ); | ||
/// ``` | ||
class DefaultFirebaseOptions { | ||
static FirebaseOptions get currentPlatform { | ||
if (kIsWeb) { | ||
throw UnsupportedError( | ||
'DefaultFirebaseOptions have not been configured for web - ' | ||
'you can reconfigure this by running the FlutterFire CLI again.', | ||
); | ||
} | ||
switch (defaultTargetPlatform) { | ||
case TargetPlatform.android: | ||
return android; | ||
case TargetPlatform.iOS: | ||
throw UnsupportedError( | ||
'DefaultFirebaseOptions have not been configured for ios - ' | ||
'you can reconfigure this by running the FlutterFire CLI again.', | ||
); | ||
case TargetPlatform.macOS: | ||
throw UnsupportedError( | ||
'DefaultFirebaseOptions have not been configured for macos - ' | ||
'you can reconfigure this by running the FlutterFire CLI again.', | ||
); | ||
case TargetPlatform.windows: | ||
throw UnsupportedError( | ||
'DefaultFirebaseOptions have not been configured for windows - ' | ||
'you can reconfigure this by running the FlutterFire CLI again.', | ||
); | ||
case TargetPlatform.linux: | ||
throw UnsupportedError( | ||
'DefaultFirebaseOptions have not been configured for linux - ' | ||
'you can reconfigure this by running the FlutterFire CLI again.', | ||
); | ||
default: | ||
throw UnsupportedError( | ||
'DefaultFirebaseOptions are not supported for this platform.', | ||
); | ||
} | ||
} | ||
|
||
static const FirebaseOptions android = FirebaseOptions( | ||
apiKey: 'AIzaSyATmGPGCnhEzncmC9lYhixCTn0jvXrhykA', | ||
appId: '1:788037853247:android:c1e473148d4ba293e7988a', | ||
messagingSenderId: '788037853247', | ||
projectId: 'attendance-338eb', | ||
storageBucket: 'attendance-338eb.appspot.com', | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'dart:convert'; | ||
import 'package:hive/hive.dart'; | ||
|
||
class LocalStorage { | ||
static final _localStorage = Hive.box('localStorage'); | ||
static addNotification(notification){ | ||
var rawData=_localStorage.get("notifications"); | ||
if(rawData!=null){ | ||
List previousNotifications= jsonDecode(rawData); | ||
previousNotifications.add(notification); | ||
_localStorage.put("notifications",jsonEncode(previousNotifications)); | ||
} | ||
else{ | ||
List emptyNotifications=[]; | ||
emptyNotifications.add(notification); | ||
_localStorage.put("notifications",jsonEncode(emptyNotifications)); | ||
} | ||
} | ||
|
||
static getNotifications(){ | ||
var rawData=_localStorage.get("notifications"); | ||
if(rawData!=null){ | ||
return jsonDecode(rawData); | ||
} | ||
else{ | ||
return []; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import 'dart:convert'; | ||
import 'package:firebase_messaging/firebase_messaging.dart'; | ||
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; | ||
import 'package:http/http.dart' as http; | ||
class NotificationConfig { | ||
static final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = | ||
FlutterLocalNotificationsPlugin(); | ||
static const AndroidNotificationChannel channel = AndroidNotificationChannel( | ||
'pclub-notification', | ||
'pclub-ui-notification', | ||
description: 'This channel is used for important notifications.', | ||
importance: Importance.max, | ||
); | ||
static Future initialize(context) async { | ||
var androidInitialize = | ||
const AndroidInitializationSettings('mipmap/ic_noti'); | ||
var initializationsSettings = | ||
InitializationSettings(android: androidInitialize); | ||
await flutterLocalNotificationsPlugin | ||
.resolvePlatformSpecificImplementation< | ||
AndroidFlutterLocalNotificationsPlugin>() | ||
?.createNotificationChannel(channel); | ||
await flutterLocalNotificationsPlugin.initialize(initializationsSettings); | ||
} | ||
|
||
static Future showNotification(RemoteMessage message) async { | ||
late AndroidNotificationDetails androidPlatformChannelSpecifics; | ||
if(message.notification!.android!.imageUrl!=null && message.notification!.android!.imageUrl!=""){ | ||
final http.Response response = await http.get(Uri.parse(message.notification!.android!.imageUrl!)); | ||
BigPictureStyleInformation bigPictureStyleInformation = | ||
BigPictureStyleInformation( | ||
ByteArrayAndroidBitmap.fromBase64String(base64Encode(response.bodyBytes)), | ||
largeIcon: ByteArrayAndroidBitmap.fromBase64String(base64Encode(response.bodyBytes)), | ||
); | ||
androidPlatformChannelSpecifics = AndroidNotificationDetails( | ||
channel.id, | ||
channel.name, | ||
playSound: true, | ||
icon: "mipmap/ic_noti", | ||
importance: Importance.max, | ||
priority: Priority.max, | ||
styleInformation: bigPictureStyleInformation, | ||
largeIcon: ByteArrayAndroidBitmap.fromBase64String( | ||
base64Encode(response.bodyBytes)) | ||
); | ||
}else{ | ||
androidPlatformChannelSpecifics = AndroidNotificationDetails( | ||
channel.id, | ||
channel.name, | ||
playSound: true, | ||
icon: "mipmap/ic_noti", | ||
importance: Importance.max, | ||
priority: Priority.max | ||
); | ||
} | ||
|
||
var not = NotificationDetails(android: androidPlatformChannelSpecifics); | ||
await flutterLocalNotificationsPlugin.show(0,message.notification!.title , message.notification!.body, not, | ||
payload: jsonEncode(message.data)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import 'package:hive_flutter/hive_flutter.dart'; | ||
|
||
class Storage { | ||
static Future<void> initialize() async { | ||
await Hive.initFlutter(); | ||
await Hive.openBox('localStorage'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.