main.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import 'package:firebase_core/firebase_core.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:latlong2/latlong.dart';
  4. import 'navigation/navigation_page.dart';
  5. import 'challenges/challenge_page.dart';
  6. import 'package:firebase_messaging/firebase_messaging.dart';
  7. import 'package:cloud_firestore/cloud_firestore.dart';
  8. import 'firebase_options.dart';
  9. void main() async {
  10. WidgetsFlutterBinding.ensureInitialized();
  11. await Firebase.initializeApp(
  12. options: DefaultFirebaseOptions.currentPlatform,
  13. );
  14. handleMessages();
  15. runApp(const PhysiGo());
  16. }
  17. class PhysiGo extends StatelessWidget {
  18. const PhysiGo({Key? key}) : super(key: key);
  19. @override
  20. Widget build(BuildContext context) {
  21. return MaterialApp(
  22. title: 'PhysiGo',
  23. theme: ThemeData(
  24. primarySwatch: Colors.blue,
  25. ),
  26. home: const HomePage(),
  27. );
  28. }
  29. }
  30. class HomePage extends StatelessWidget {
  31. const HomePage({Key? key}) : super(key: key);
  32. @override
  33. Widget build(BuildContext context) {
  34. return Scaffold(
  35. body: Column(
  36. mainAxisAlignment: MainAxisAlignment.center,
  37. crossAxisAlignment: CrossAxisAlignment.center,
  38. children: [Center(
  39. child: TextButton(
  40. onPressed: () {
  41. Navigator.push(
  42. context,
  43. MaterialPageRoute(
  44. // Example on how to use NavigationPage
  45. builder: (context) => NavigationPage(
  46. destination: LatLng(51.78036111980833, 19.451262207821234),
  47. ),
  48. ),
  49. );
  50. },
  51. child: const Text('Navigation')),
  52. ),
  53. Center(
  54. child: TextButton(
  55. onPressed: () {
  56. Navigator.push(
  57. context,
  58. MaterialPageRoute(
  59. // Example on how to use NavigationPage
  60. builder: (context) => const ChallengePage(),
  61. ),
  62. );
  63. },
  64. child: const Text('Challenges')),
  65. ),
  66. ],
  67. ));
  68. }
  69. }
  70. void handleMessages() async {
  71. FirebaseMessaging messaging = FirebaseMessaging.instance;
  72. final _firestore = FirebaseFirestore.instance;
  73. NotificationSettings settings = await messaging.requestPermission(
  74. alert: true,
  75. announcement: false,
  76. badge: true,
  77. carPlay: false,
  78. criticalAlert: false,
  79. provisional: false,
  80. sound: true,
  81. );
  82. FirebaseMessaging.onMessage.listen((RemoteMessage message) {
  83. print('Got a message whilst in the foreground!');
  84. print('Message data: ${message.data}');
  85. if (message.notification != null) {
  86. print('Message also contained a notification: ${message.notification}');
  87. }
  88. });
  89. messaging.onTokenRefresh.listen((fcmToken) async {
  90. final fcmToken = await messaging.getToken();
  91. final user = _firestore.collection('Users').doc('tlmysIvwTBaoZKWqBofx');
  92. user.update({
  93. 'token': fcmToken,
  94. });
  95. });
  96. }