Просмотр исходного кода

feat: display push notifications

Léo Salé 3 лет назад
Родитель
Сommit
8283c3bc2b

+ 1 - 1
app/android/app/build.gradle

@@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
 apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
 apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
 
 
 android {
 android {
-    compileSdkVersion flutter.compileSdkVersion
+    compileSdkVersion 31
 
 
     compileOptions {
     compileOptions {
         sourceCompatibility JavaVersion.VERSION_1_8
         sourceCompatibility JavaVersion.VERSION_1_8

+ 8 - 9
app/android/app/src/main/AndroidManifest.xml

@@ -20,19 +20,18 @@
                  the Android process has started. This theme is visible to the user
                  the Android process has started. This theme is visible to the user
                  while the Flutter UI initializes. After that, this theme continues
                  while the Flutter UI initializes. After that, this theme continues
                  to determine the Window background behind the Flutter UI. -->
                  to determine the Window background behind the Flutter UI. -->
-            <meta-data
-              android:name="io.flutter.embedding.android.NormalTheme"
-              android:resource="@style/NormalTheme"
-              />
+            <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" />
             <intent-filter>
             <intent-filter>
-                <action android:name="android.intent.action.MAIN"/>
-                <category android:name="android.intent.category.LAUNCHER"/>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
             </intent-filter>
+            <!-- <intent-filter>
+                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter> -->
         </activity>
         </activity>
         <!-- Don't delete the meta-data below.
         <!-- Don't delete the meta-data below.
              This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
              This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
-        <meta-data
-            android:name="flutterEmbedding"
-            android:value="2" />
+        <meta-data android:name="flutterEmbedding" android:value="2" />
     </application>
     </application>
 </manifest>
 </manifest>

+ 3 - 0
app/lib/main.dart

@@ -6,12 +6,15 @@ import 'package:physigo/friends/friends_page.dart';
 import 'navigation/navigation_page.dart';
 import 'navigation/navigation_page.dart';
 
 
 import 'firebase_options.dart';
 import 'firebase_options.dart';
+import 'services/push_notifications.dart';
 
 
 void main() async {
 void main() async {
   WidgetsFlutterBinding.ensureInitialized();
   WidgetsFlutterBinding.ensureInitialized();
   await Firebase.initializeApp(
   await Firebase.initializeApp(
     options: DefaultFirebaseOptions.currentPlatform,
     options: DefaultFirebaseOptions.currentPlatform,
   );
   );
+  handleMessages();
+
   runApp(const PhysiGo());
   runApp(const PhysiGo());
 }
 }
 
 

+ 70 - 0
app/lib/services/push_notifications.dart

@@ -0,0 +1,70 @@
+import 'package:awesome_notifications/awesome_notifications.dart';
+import 'package:cloud_firestore/cloud_firestore.dart';
+import 'package:firebase_messaging/firebase_messaging.dart';
+import 'package:flutter/foundation.dart';
+import 'package:flutter/material.dart';
+
+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,
+  );
+
+  localNotificationsSetup();
+
+  if (settings.authorizationStatus == AuthorizationStatus.denied) {
+    return;
+  }
+
+  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
+    AwesomeNotifications().createNotification(
+      content: NotificationContent(
+        id: 10,
+        channelKey: 'basic_channel',
+        title: message.notification?.title,
+        body: message.notification?.body,
+        color: Colors.blue,
+      )
+    );
+  });
+
+  messaging.onTokenRefresh.listen((fcmToken) async {
+    final fcmToken = await messaging.getToken();
+    final user = _firestore.collection('Users').doc('tlmysIvwTBaoZKWqBofx');
+    user.update({
+      'token': fcmToken,
+    });
+  });
+}
+
+void localNotificationsSetup() {
+  AwesomeNotifications().initialize(
+      // set the icon to null if you want to use the default app icon
+      null,
+      [
+        NotificationChannel(
+          channelGroupKey: 'basic_channel_group',
+          channelKey: 'basic_channel',
+          channelName: 'Basic notifications',
+          channelDescription: 'Notification channel for basic tests',
+        )
+      ],
+      debug: kDebugMode);
+
+  AwesomeNotifications().isNotificationAllowed().then((isAllowed) {
+    if (!isAllowed) {
+      // This is just a basic example. For real apps, you must show some
+      // friendly dialog box before call the request method.
+      // This is very important to not harm the user experience
+      AwesomeNotifications().requestPermissionToSendNotifications();
+    }
+  });
+}

+ 35 - 0
app/pubspec.lock

@@ -8,6 +8,13 @@ packages:
       url: "https://pub.dartlang.org"
       url: "https://pub.dartlang.org"
     source: hosted
     source: hosted
     version: "2.8.2"
     version: "2.8.2"
+  awesome_notifications:
+    dependency: "direct main"
+    description:
+      name: awesome_notifications
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "0.6.21"
   body_detection:
   body_detection:
     dependency: "direct main"
     dependency: "direct main"
     description:
     description:
@@ -106,6 +113,27 @@ packages:
       url: "https://pub.dartlang.org"
       url: "https://pub.dartlang.org"
     source: hosted
     source: hosted
     version: "1.6.4"
     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:
   flutter:
     dependency: "direct main"
     dependency: "direct main"
     description: flutter
     description: flutter
@@ -310,6 +338,13 @@ packages:
       url: "https://pub.dartlang.org"
       url: "https://pub.dartlang.org"
     source: hosted
     source: hosted
     version: "0.1.0"
     version: "0.1.0"
+  platform:
+    dependency: transitive
+    description:
+      name: platform
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "3.1.0"
   plugin_platform_interface:
   plugin_platform_interface:
     dependency: transitive
     dependency: transitive
     description:
     description:

+ 2 - 0
app/pubspec.yaml

@@ -42,6 +42,8 @@ dependencies:
   body_detection: ^0.0.3
   body_detection: ^0.0.3
   rxdart: ^0.27.3
   rxdart: ^0.27.3
   cloud_firestore: ^3.1.15
   cloud_firestore: ^3.1.15
+  firebase_messaging: ^11.4.0
+  awesome_notifications: ^0.6.21
 
 
 dev_dependencies:
 dev_dependencies:
   flutter_test:
   flutter_test: