diff --git a/.fandogh/fandogh.yaml b/.fandogh/fandogh.yaml index 98722a9..7f5edca 100644 --- a/.fandogh/fandogh.yaml +++ b/.fandogh/fandogh.yaml @@ -3,7 +3,7 @@ name: app-test spec: allow_http: false disable_default_domains: true - image: app:2.1.1.18 + image: app:2.1.1.181 image_pull_policy: IfNotPresent path: / replicas: 1 diff --git a/lib/providers/user.dart b/lib/providers/user.dart index eb29f80..428233a 100644 --- a/lib/providers/user.dart +++ b/lib/providers/user.dart @@ -46,13 +46,15 @@ class UserProvider extends CoreProvier { value: service.result['user']['end'], ); AppInitializer.initializeFirebase().then((_) => _registerFirebaseToken()); - _registerFirebaseToken(); return true; } throw 'Getting user from API failed!'; } Future _registerFirebaseToken() async { + if (AppInitializer.fcmToken == null) { + return; + } final service = RequestService(RequestHelper.firebaseToken, body: { 'token': AppInitializer.fcmToken, }); diff --git a/lib/services/app_initalizer.dart b/lib/services/app_initalizer.dart index 749460e..b8a2378 100644 --- a/lib/services/app_initalizer.dart +++ b/lib/services/app_initalizer.dart @@ -62,19 +62,32 @@ class AppInitializer { static Future initializeFirebase() async { try { await Firebase.initializeApp( - options: const FirebaseOptions( - apiKey: 'AIzaSyBp-UHjWeM0H0UHtX5yguFKG-riMzvvCzw', - appId: '1:935017686266:android:f9cbc9aba8e3d65ed2d543', - messagingSenderId: '935017686266', - projectId: 'didvan-9b7da', - ), + options: kIsWeb + ? const FirebaseOptions( + apiKey: "AIzaSyA0HZjKpRuPOi1SC3f_EZTvlS3mcj9UVo0", + authDomain: "didvan-9b7da.firebaseapp.com", + projectId: "didvan-9b7da", + storageBucket: "didvan-9b7da.appspot.com", + messagingSenderId: "935017686266", + appId: "1:935017686266:web:a93f7a19bed23c51d2d543", + measurementId: "G-80B4H9E8Y0") + : const FirebaseOptions( + apiKey: 'AIzaSyBp-UHjWeM0H0UHtX5yguFKG-riMzvvCzw', + appId: '1:935017686266:android:f9cbc9aba8e3d65ed2d543', + messagingSenderId: '935017686266', + projectId: 'didvan-9b7da', + ), ); } catch (e) { Firebase.app(); } final FirebaseMessaging fcm = FirebaseMessaging.instance; - fcmToken = await fcm.getToken(); - await fcm.subscribeToTopic('general'); + fcmToken = await fcm.getToken( + vapidKey: kIsWeb + ? 'BMXHGd93t_htpS7c62ceuuLVVmia2cEDmqxp46g9Vt0B3OxNMKIqN9nupsUMtv2Vq8Yy2sQGIqgCm9FxUSKvssU' + : null, + ); + // await fcm.subscribeToTopic('general'); await fcm.requestPermission( alert: true, announcement: false, diff --git a/lib/views/splash/splash.dart b/lib/views/splash/splash.dart index 27943c1..d0b8595 100644 --- a/lib/views/splash/splash.dart +++ b/lib/views/splash/splash.dart @@ -110,7 +110,9 @@ class _SplashState extends State { final String? token = await userProvider.setAndGetToken(); if (token != null) { log(token); - context.read().getDownloadsList(); + if (!kIsWeb) { + context.read().getDownloadsList(); + } RequestService.token = token; final result = await userProvider.getUserInfo(); if (!result) { diff --git a/web/firebase-messaging-sw.js b/web/firebase-messaging-sw.js new file mode 100644 index 0000000..97f89d2 --- /dev/null +++ b/web/firebase-messaging-sw.js @@ -0,0 +1,41 @@ +importScripts("https://www.gstatic.com/firebasejs/7.15.5/firebase-app.js"); +importScripts("https://www.gstatic.com/firebasejs/7.15.5/firebase-messaging.js"); + +//Using singleton breaks instantiating messaging() +// App firebase = FirebaseWeb.instance.app; + + +const firebaseConfig = { + apiKey: "AIzaSyA0HZjKpRuPOi1SC3f_EZTvlS3mcj9UVo0", + authDomain: "didvan-9b7da.firebaseapp.com", + projectId: "didvan-9b7da", + storageBucket: "didvan-9b7da.appspot.com", + messagingSenderId: "935017686266", + appId: "1:935017686266:web:a93f7a19bed23c51d2d543", + measurementId: "G-80B4H9E8Y0" +}; + + +console.log(firebase, 'kireomidyekamshaghshode'); +const init = firebase.initializeApp(firebaseConfig); +const messaging = firebase.messaging(); +messaging.setBackgroundMessageHandler(function (payload) { + const promiseChain = clients + .matchAll({ + type: "window", + includeUncontrolled: true + }) + .then(windowClients => { + for (let i = 0; i < windowClients.length; i++) { + const windowClient = windowClients[i]; + windowClient.postMessage(payload); + } + }) + .then(() => { + return registration.showNotification("New Message"); + }); + return promiseChain; +}); +self.addEventListener('notificationclick', function (event) { + console.log('notification received: ', event) +}); \ No newline at end of file