|
|
@@ -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();
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|