Procházet zdrojové kódy

Receive push notifications

Marcin Jaborski před 3 roky
rodič
revize
ba90a005ac

+ 5 - 0
app/android/app/src/main/AndroidManifest.xml

@@ -27,6 +27,11 @@
                 <action android:name="android.intent.action.MAIN"/>
                 <category android:name="android.intent.category.LAUNCHER"/>
             </intent-filter>
+
+            <intent-filter>
+                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
         </activity>
         <!-- Don't delete the meta-data below.
              This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->

+ 52 - 15
app/lib/main.dart

@@ -3,6 +3,8 @@ import 'package:flutter/material.dart';
 import 'package:latlong2/latlong.dart';
 import 'navigation/navigation_page.dart';
 import 'challenges/challenge_page.dart';
+import 'package:firebase_messaging/firebase_messaging.dart';
+import 'package:cloud_firestore/cloud_firestore.dart';
 
 import 'firebase_options.dart';
 
@@ -11,11 +13,13 @@ void main() async {
   await Firebase.initializeApp(
     options: DefaultFirebaseOptions.currentPlatform,
   );
+  handleMessages();
   runApp(const PhysiGo());
 }
 
 class PhysiGo extends StatelessWidget {
   const PhysiGo({Key? key}) : super(key: key);
+
   @override
   Widget build(BuildContext context) {
     return MaterialApp(
@@ -52,20 +56,53 @@ class HomePage extends StatelessWidget {
               },
               child: const Text('Navigation')),
         ),
-          Center(
-            child: TextButton(
-                onPressed: () {
-                  Navigator.push(
-                    context,
-                    MaterialPageRoute(
-                      // Example on how to use NavigationPage
-                      builder: (context) => const ChallengePage(),
-                    ),
-                  );
-                },
-                child: const Text('Challenges')),
-          ),],
-      )
-    );
+        Center(
+          child: TextButton(
+              onPressed: () {
+                Navigator.push(
+                  context,
+                  MaterialPageRoute(
+                    // Example on how to use NavigationPage
+                    builder: (context) => const ChallengePage(),
+                  ),
+                );
+              },
+              child: const Text('Challenges')),
+        ),
+      ],
+    ));
   }
 }
+
+void handleMessages() async {
+  FirebaseMessaging messaging = FirebaseMessaging.instance;
+  final _firestore = FirebaseFirestore.instance;
+
+  NotificationSettings settings = await messaging.requestPermission(
+    alert: true,
+    announcement: false,
+    badge: true,
+    carPlay: false,
+    criticalAlert: false,
+    provisional: false,
+    sound: true,
+  );
+
+
+  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
+    print('Got a message whilst in the foreground!');
+    print('Message data: ${message.data}');
+
+    if (message.notification != null) {
+      print('Message also contained a notification: ${message.notification}');
+    }
+  });
+
+  messaging.onTokenRefresh.listen((fcmToken) async {
+    final fcmToken = await messaging.getToken();
+    final user = _firestore.collection('Users').doc('tlmysIvwTBaoZKWqBofx');
+    user.update({
+      'token': fcmToken,
+    });
+  });
+}

+ 23 - 2
app/pubspec.lock

@@ -91,14 +91,35 @@ packages:
       name: firebase_core_platform_interface
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "4.3.0"
+    version: "4.4.0"
   firebase_core_web:
     dependency: transitive
     description:
       name: firebase_core_web
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "1.6.3"
+    version: "1.6.4"
+  firebase_messaging:
+    dependency: "direct main"
+    description:
+      name: firebase_messaging
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "11.4.0"
+  firebase_messaging_platform_interface:
+    dependency: transitive
+    description:
+      name: firebase_messaging_platform_interface
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "3.5.0"
+  firebase_messaging_web:
+    dependency: transitive
+    description:
+      name: firebase_messaging_web
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.4.0"
   flutter:
     dependency: "direct main"
     description: flutter

+ 1 - 0
app/pubspec.yaml

@@ -40,6 +40,7 @@ dependencies:
   flutter_map_location_marker: ^3.1.0
   cloud_firestore: ^3.1.14
   geoflutterfire: ^3.0.3
+  firebase_messaging: ^11.4.0
 
 dev_dependencies:
   flutter_test: