-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
19 changed files
with
341 additions
and
243 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:firebase_auth/firebase_auth.dart'; | ||
import 'package:firebase_core/firebase_core.dart'; | ||
import 'package:firebase_messaging/firebase_messaging.dart'; | ||
import 'package:firebase_storage/firebase_storage.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
@@ -31,38 +32,24 @@ SharedPreferences? prefs; | |
final auth = FirebaseAuth.instance; | ||
final firestore = FirebaseFirestore.instance; | ||
final storage = FirebaseStorage.instance; | ||
final firebaseMessaging = FirebaseMessaging.instance; | ||
|
||
/// Main function | ||
void main() async { | ||
// Just to show errors not so rudely | ||
ErrorWidget.builder = (FlutterErrorDetails details) { | ||
return Material( | ||
child: Column( | ||
mainAxisSize: MainAxisSize.min, | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
const Text( | ||
':-( Something went wrong!', | ||
style: TextStyle(fontSize: 20), | ||
textAlign: TextAlign.center, | ||
), | ||
Text( | ||
'\n${details.exception}', | ||
style: const TextStyle(color: Colors.red), | ||
textAlign: TextAlign.center, | ||
), | ||
const Text( | ||
'Contact shivanshukgupta or sanidhayasharma141 on linkedin for support\n', | ||
textAlign: TextAlign.center, | ||
), | ||
], | ||
)); | ||
}; | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
/// Just to show errors not so rudely | ||
setErrorWidget(); | ||
|
||
// Initializing Firebase SDK | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
await Firebase.initializeApp( | ||
options: DefaultFirebaseOptions.currentPlatform, | ||
); | ||
|
||
/// Setting up firebase messaging | ||
final fcmToken = await firebaseMessaging.getToken(); | ||
debugPrint("fcmToken = $fcmToken"); | ||
initializeFCM(); | ||
|
||
// Initializing prefs here in main to avoid any delay in activating settings | ||
prefs = await SharedPreferences.getInstance(); | ||
try { | ||
|
@@ -86,6 +73,45 @@ void main() async { | |
runApp(const ProviderScope(child: HustleStayApp())); | ||
} | ||
|
||
void initializeFCM() { | ||
firebaseMessaging.onTokenRefresh.listen((fcmToken) { | ||
debugPrint("FCM Token was refreshed. The new token is: $fcmToken"); | ||
// TODO: If necessary send token to application server. | ||
|
||
// Note: This callback is fired at each app startup and whenever a new | ||
// token is generated. | ||
}).onError((err) { | ||
debugPrint("Error: $err"); | ||
// Error getting token. | ||
}); | ||
} | ||
|
||
void setErrorWidget() { | ||
ErrorWidget.builder = (FlutterErrorDetails details) { | ||
return Material( | ||
child: Column( | ||
mainAxisSize: MainAxisSize.min, | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
const Text( | ||
':-( Something went wrong!', | ||
style: TextStyle(fontSize: 20), | ||
textAlign: TextAlign.center, | ||
), | ||
Text( | ||
'\n${details.exception}', | ||
style: const TextStyle(color: Colors.red), | ||
textAlign: TextAlign.center, | ||
), | ||
const Text( | ||
'Contact shivanshukgupta or sanidhayasharma141 on linkedin for support\n', | ||
textAlign: TextAlign.center, | ||
), | ||
], | ||
)); | ||
}; | ||
} | ||
|
||
/// The main app widget | ||
class HustleStayApp extends ConsumerWidget { | ||
const HustleStayApp({super.key}); | ||
|
@@ -182,7 +208,11 @@ Future<void> initializeEverything() async { | |
await initializeComplaints(); | ||
everythingInitialized.value = "Fetching requests"; | ||
await initializeRequests(); | ||
everythingInitialized.value = "Fetching vehicle requests"; | ||
await initializeVehicleRequests(); | ||
|
||
if (currentUser.type == 'warden' || | ||
currentUser.email == '[email protected]') { | ||
everythingInitialized.value = "Fetching vehicle requests"; | ||
await initializeVehicleRequests(); | ||
} | ||
everythingInitialized.value = null; | ||
} |
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,40 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:http/http.dart' as http; | ||
|
||
Future<void> sendNotification({ | ||
String? title, | ||
String? body, | ||
String to = '/topics/all', | ||
}) async { | ||
/// TODO: Dangerous!!! the fcm API key is here and should be moved to a backend | ||
/// ERR: | ||
const String serverKey = | ||
'AAAAmUIGOT0:APA91bEVQn5IIBwUrIG8Brgf3vzZ-KxaGnDYY_8ElgZq65t909kx_EzFz6l613Kny_4Jh0JTcbm-EE3dvWGWM7dMISwseQ_wF0iYPDX9ti-nJKqrxKOXt3sKtXWh-VXSX_e3fsapadQO'; | ||
const String fcmUrl = 'https://fcm.googleapis.com/fcm/send'; | ||
|
||
final Map<String, dynamic> notification = { | ||
'to': to, | ||
'notification': { | ||
'title': title, | ||
'body': body, | ||
}, | ||
}; | ||
|
||
final headers = { | ||
'Content-Type': 'application/json', | ||
'Authorization': 'key=$serverKey', | ||
}; | ||
|
||
final response = await http.post( | ||
Uri.parse(fcmUrl), | ||
headers: headers, | ||
body: jsonEncode(notification), | ||
); | ||
|
||
if (response.statusCode == 200) { | ||
print('Notification sent successfully'); | ||
} else { | ||
print('Failed to send notification: ${response.body}'); | ||
} | ||
} |
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
Oops, something went wrong.