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.

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