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.

240 lines
7.2 KiB

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:overlay_support/overlay_support.dart';
import '/database/models/my_profile.dart';
import '/views/main/home.dart';
import '/views/authentication/unauthenticated_landing.dart';
import '/views/authentication/login.dart';
import '/views/authentication/signup.dart';
class PushNotification {
PushNotification({
this.title,
this.body,
this.dataTitle,
this.dataBody,
});
String? title;
String? body;
String? dataTitle;
String? dataBody;
}
void main() async {
await dotenv.load(fileName: '.env');
// TODO: Replace this with the prod url when server is deployed
MyProfile.setServerUrl(dotenv.env['SERVER_URL'] ?? 'http://localhost:8080/');
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);
static const String _title = 'Envelope';
late final FirebaseMessaging _messaging;
void registerNotification() async {
await Firebase.initializeApp();
_messaging = FirebaseMessaging.instance;
NotificationSettings settings = await _messaging.requestPermission(
alert: true,
badge: true,
provisional: false,
sound: true,
);
if (settings.authorizationStatus != AuthorizationStatus.authorized) {
return;
}
final fcmToken = await _messaging.getToken();
print('Device Token: $fcmToken');
Color notificationBg = Colors.grey.shade100;
if (SchedulerBinding.instance.window.platformBrightness == Brightness.dark) {
notificationBg = Colors.grey[850]!;
}
// For handling the received notifications
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
// Parse the message received
PushNotification notification = PushNotification(
title: message.notification?.title,
body: message.notification?.body,
);
// TODO: Replace this with a better notification UI
showSimpleNotification(
Text(notification.title!),
// leading: NotificationBadge(totalNotifications: _totalNotifications),
background: notificationBg,
duration: const Duration(seconds: 5),
slideDismissDirection: DismissDirection.up,
);
});
}
@override
Widget build(BuildContext context) {
registerNotification();
return OverlaySupport.global(
child: MaterialApp(
title: _title,
routes: {
'/home': (context) => const Home(),
'/landing': (context) => const UnauthenticatedLandingWidget(),
'/login': (context) => const Login(),
'/signup': (context) => const Signup(),
},
home: const Scaffold(
body: SafeArea(
child: Home(),
)
),
theme: ThemeData(
brightness: Brightness.light,
primaryColor: const Color(0xff014bff),
backgroundColor: Colors.grey[300],
scaffoldBackgroundColor: Colors.grey.shade100,
disabledColor: Colors.grey[700],
hintColor: Colors.grey.shade700,
colorScheme: ColorScheme(
brightness: Brightness.light,
primary: const Color(0xff014bff),
onPrimary: Colors.white,
secondary: const Color(0xff1a6dff),
onSecondary: Colors.black,
tertiary: const Color(0xff3490ff),
onTertiary: Colors.black,
error: Colors.red,
onError: Colors.white,
background: Colors.grey.shade300,
onBackground: Colors.black,
surface: Colors.grey.shade100,
onSurface: Colors.black,
),
appBarTheme: AppBarTheme(
color: Colors.grey.shade100,
foregroundColor: Colors.white,
iconTheme: const IconThemeData(
color: Colors.black,
),
toolbarTextStyle: const TextStyle(
color: Colors.black,
),
systemOverlayStyle: const SystemUiOverlayStyle(
statusBarColor: Colors.black,
statusBarIconBrightness: Brightness.light,
statusBarBrightness: Brightness.light,
)
),
iconTheme: const IconThemeData(color: Colors.black),
inputDecorationTheme: InputDecorationTheme(
filled: true,
fillColor: Colors.white,
labelStyle: const TextStyle(
color: Colors.black,
fontSize: 30,
),
hintStyle: TextStyle(
color: Colors.grey.shade600,
),
iconColor: Colors.grey.shade500,
contentPadding: const EdgeInsets.all(8),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(15),
borderSide: const BorderSide(
color: Colors.transparent,
)
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(15),
borderSide: const BorderSide(
color: Colors.transparent,
)
),
),
),
darkTheme: ThemeData(
brightness: Brightness.dark,
primaryColor: const Color(0xffff4a27),
backgroundColor: Colors.grey.shade800,
scaffoldBackgroundColor: Colors.grey[850],
disabledColor: Colors.grey[400],
hintColor: Colors.grey.shade400,
colorScheme: ColorScheme(
brightness: Brightness.dark,
primary: const Color(0xffff4a27),
onPrimary: Colors.white,
secondary: const Color(0xffff5f3a),
onSecondary: Colors.white,
tertiary: const Color(0xffff7950),
onTertiary: Colors.black,
error: Colors.red,
onError: Colors.white,
background: Colors.grey.shade900,
onBackground: Colors.white,
surface: Colors.grey.shade700,
onSurface: Colors.white,
),
appBarTheme: AppBarTheme(
color: Colors.grey.shade800,
iconTheme: IconThemeData(
color: Colors.grey.shade400
),
toolbarTextStyle: TextStyle(
color: Colors.grey.shade400
),
),
iconTheme: const IconThemeData(color: Colors.white),
inputDecorationTheme: InputDecorationTheme(
filled: true,
fillColor: Colors.grey.shade800,
hintStyle: TextStyle(
color: Colors.grey.shade500,
),
iconColor: Colors.grey.shade500,
contentPadding: const EdgeInsets.all(8),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(15),
borderSide: const BorderSide(
color: Colors.transparent,
)
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(15),
borderSide: const BorderSide(
color: Colors.transparent,
)
),
),
),
),
);
}
}