Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
7.4 KiB

  1. import 'package:firebase_core/firebase_core.dart';
  2. import 'package:firebase_messaging/firebase_messaging.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/scheduler.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:flutter_dotenv/flutter_dotenv.dart';
  7. import 'package:overlay_support/overlay_support.dart';
  8. import '/database/models/my_profile.dart';
  9. import '/views/main/home.dart';
  10. import '/views/authentication/unauthenticated_landing.dart';
  11. import '/views/authentication/login.dart';
  12. import '/views/authentication/signup.dart';
  13. class PushNotification {
  14. PushNotification({
  15. this.title,
  16. this.body,
  17. this.dataTitle,
  18. this.dataBody,
  19. });
  20. String? title;
  21. String? body;
  22. String? dataTitle;
  23. String? dataBody;
  24. }
  25. void main() async {
  26. await dotenv.load(fileName: '.env');
  27. // TODO: Replace this with the prod url when server is deployed
  28. MyProfile.setServerUrl(dotenv.env['SERVER_URL'] ?? 'http://localhost:8080/');
  29. runApp(MyApp());
  30. }
  31. class MyApp extends StatelessWidget {
  32. MyApp({Key? key}) : super(key: key);
  33. static const String _title = 'Envelope';
  34. late final FirebaseMessaging _messaging;
  35. void registerNotification() async {
  36. await Firebase.initializeApp();
  37. _messaging = FirebaseMessaging.instance;
  38. NotificationSettings settings = await _messaging.requestPermission(
  39. alert: true,
  40. badge: true,
  41. provisional: false,
  42. sound: true,
  43. );
  44. if (settings.authorizationStatus != AuthorizationStatus.authorized) {
  45. return;
  46. }
  47. final fcmToken = await _messaging.getToken();
  48. print('Device Token: $fcmToken');
  49. Color notificationBg = Colors.grey.shade100;
  50. if (SchedulerBinding.instance.window.platformBrightness == Brightness.dark) {
  51. notificationBg = Colors.grey[850]!;
  52. }
  53. // For handling the received notifications
  54. FirebaseMessaging.onMessage.listen((RemoteMessage message) {
  55. // Parse the message received
  56. PushNotification notification = PushNotification(
  57. title: message.notification?.title,
  58. body: message.notification?.body,
  59. );
  60. // TODO: Replace this with a better notification UI
  61. showSimpleNotification(
  62. Text(notification.title!),
  63. // leading: NotificationBadge(totalNotifications: _totalNotifications),
  64. background: notificationBg,
  65. duration: const Duration(seconds: 5),
  66. slideDismissDirection: DismissDirection.up,
  67. );
  68. });
  69. }
  70. @override
  71. Widget build(BuildContext context) {
  72. registerNotification();
  73. return OverlaySupport.global(
  74. child: MaterialApp(
  75. title: _title,
  76. routes: {
  77. '/home': (context) => const Home(),
  78. '/landing': (context) => const UnauthenticatedLandingWidget(),
  79. '/login': (context) => const Login(),
  80. '/signup': (context) => const Signup(),
  81. },
  82. home: const Scaffold(
  83. body: SafeArea(
  84. child: Home(),
  85. )
  86. ),
  87. theme: ThemeData(
  88. brightness: Brightness.light,
  89. primaryColor: const Color(0xff014bff),
  90. backgroundColor: Colors.grey[300],
  91. scaffoldBackgroundColor: Colors.grey.shade100,
  92. disabledColor: Colors.grey[700],
  93. hintColor: Colors.grey.shade700,
  94. colorScheme: ColorScheme(
  95. brightness: Brightness.light,
  96. primary: const Color(0xff014bff),
  97. onPrimary: Colors.white,
  98. secondary: const Color(0xff1a6dff),
  99. onSecondary: Colors.black,
  100. tertiary: const Color(0xff3490ff),
  101. onTertiary: Colors.black,
  102. error: Colors.red,
  103. onError: Colors.white,
  104. background: Colors.grey.shade300,
  105. onBackground: Colors.black,
  106. surface: Colors.grey.shade100,
  107. onSurface: Colors.black,
  108. ),
  109. appBarTheme: AppBarTheme(
  110. color: Colors.grey.shade100,
  111. foregroundColor: Colors.white,
  112. iconTheme: const IconThemeData(
  113. color: Colors.black,
  114. ),
  115. toolbarTextStyle: const TextStyle(
  116. color: Colors.black,
  117. ),
  118. systemOverlayStyle: const SystemUiOverlayStyle(
  119. statusBarColor: Colors.black,
  120. statusBarIconBrightness: Brightness.light,
  121. statusBarBrightness: Brightness.light,
  122. )
  123. ),
  124. iconTheme: const IconThemeData(color: Colors.black),
  125. inputDecorationTheme: InputDecorationTheme(
  126. filled: true,
  127. fillColor: Colors.white,
  128. labelStyle: const TextStyle(
  129. color: Colors.black,
  130. fontSize: 30,
  131. ),
  132. hintStyle: TextStyle(
  133. color: Colors.grey.shade600,
  134. ),
  135. iconColor: Colors.grey.shade500,
  136. contentPadding: const EdgeInsets.all(8),
  137. enabledBorder: OutlineInputBorder(
  138. borderRadius: BorderRadius.circular(15),
  139. borderSide: const BorderSide(
  140. color: Colors.transparent,
  141. )
  142. ),
  143. focusedBorder: OutlineInputBorder(
  144. borderRadius: BorderRadius.circular(15),
  145. borderSide: const BorderSide(
  146. color: Colors.transparent,
  147. )
  148. ),
  149. ),
  150. ),
  151. darkTheme: ThemeData(
  152. brightness: Brightness.dark,
  153. primaryColor: const Color(0xffff4a27),
  154. backgroundColor: Colors.grey.shade800,
  155. scaffoldBackgroundColor: Colors.grey[850],
  156. disabledColor: Colors.grey[400],
  157. hintColor: Colors.grey.shade400,
  158. colorScheme: ColorScheme(
  159. brightness: Brightness.dark,
  160. primary: const Color(0xffff4a27),
  161. onPrimary: Colors.white,
  162. secondary: const Color(0xffff5f3a),
  163. onSecondary: Colors.white,
  164. tertiary: const Color(0xffff7950),
  165. onTertiary: Colors.black,
  166. error: Colors.red,
  167. onError: Colors.white,
  168. background: Colors.grey.shade900,
  169. onBackground: Colors.white,
  170. surface: Colors.grey.shade700,
  171. onSurface: Colors.white,
  172. ),
  173. appBarTheme: AppBarTheme(
  174. color: Colors.grey.shade800,
  175. iconTheme: IconThemeData(
  176. color: Colors.grey.shade400
  177. ),
  178. toolbarTextStyle: TextStyle(
  179. color: Colors.grey.shade400
  180. ),
  181. systemOverlayStyle: const SystemUiOverlayStyle(
  182. statusBarColor: Colors.black,
  183. statusBarIconBrightness: Brightness.dark,
  184. statusBarBrightness: Brightness.dark,
  185. )
  186. ),
  187. iconTheme: const IconThemeData(color: Colors.white),
  188. inputDecorationTheme: InputDecorationTheme(
  189. filled: true,
  190. fillColor: Colors.grey.shade800,
  191. hintStyle: TextStyle(
  192. color: Colors.grey.shade500,
  193. ),
  194. iconColor: Colors.grey.shade500,
  195. contentPadding: const EdgeInsets.all(8),
  196. enabledBorder: OutlineInputBorder(
  197. borderRadius: BorderRadius.circular(15),
  198. borderSide: const BorderSide(
  199. color: Colors.transparent,
  200. )
  201. ),
  202. focusedBorder: OutlineInputBorder(
  203. borderRadius: BorderRadius.circular(15),
  204. borderSide: const BorderSide(
  205. color: Colors.transparent,
  206. )
  207. ),
  208. ),
  209. ),
  210. ),
  211. );
  212. }
  213. }