web push notification bug fixed

This commit is contained in:
MohammadTaha Basiri 2022-05-20 13:42:11 +04:30
parent f841c12f7a
commit 3e17f6da48
5 changed files with 69 additions and 11 deletions

View File

@ -3,7 +3,7 @@ name: app-test
spec: spec:
allow_http: false allow_http: false
disable_default_domains: true disable_default_domains: true
image: app:2.1.1.18 image: app:2.1.1.181
image_pull_policy: IfNotPresent image_pull_policy: IfNotPresent
path: / path: /
replicas: 1 replicas: 1

View File

@ -46,13 +46,15 @@ class UserProvider extends CoreProvier {
value: service.result['user']['end'], value: service.result['user']['end'],
); );
AppInitializer.initializeFirebase().then((_) => _registerFirebaseToken()); AppInitializer.initializeFirebase().then((_) => _registerFirebaseToken());
_registerFirebaseToken();
return true; return true;
} }
throw 'Getting user from API failed!'; throw 'Getting user from API failed!';
} }
Future<void> _registerFirebaseToken() async { Future<void> _registerFirebaseToken() async {
if (AppInitializer.fcmToken == null) {
return;
}
final service = RequestService(RequestHelper.firebaseToken, body: { final service = RequestService(RequestHelper.firebaseToken, body: {
'token': AppInitializer.fcmToken, 'token': AppInitializer.fcmToken,
}); });

View File

@ -62,19 +62,32 @@ class AppInitializer {
static Future<void> initializeFirebase() async { static Future<void> initializeFirebase() async {
try { try {
await Firebase.initializeApp( await Firebase.initializeApp(
options: const FirebaseOptions( options: kIsWeb
apiKey: 'AIzaSyBp-UHjWeM0H0UHtX5yguFKG-riMzvvCzw', ? const FirebaseOptions(
appId: '1:935017686266:android:f9cbc9aba8e3d65ed2d543', apiKey: "AIzaSyA0HZjKpRuPOi1SC3f_EZTvlS3mcj9UVo0",
messagingSenderId: '935017686266', authDomain: "didvan-9b7da.firebaseapp.com",
projectId: 'didvan-9b7da', 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) { } catch (e) {
Firebase.app(); Firebase.app();
} }
final FirebaseMessaging fcm = FirebaseMessaging.instance; final FirebaseMessaging fcm = FirebaseMessaging.instance;
fcmToken = await fcm.getToken(); fcmToken = await fcm.getToken(
await fcm.subscribeToTopic('general'); vapidKey: kIsWeb
? 'BMXHGd93t_htpS7c62ceuuLVVmia2cEDmqxp46g9Vt0B3OxNMKIqN9nupsUMtv2Vq8Yy2sQGIqgCm9FxUSKvssU'
: null,
);
// await fcm.subscribeToTopic('general');
await fcm.requestPermission( await fcm.requestPermission(
alert: true, alert: true,
announcement: false, announcement: false,

View File

@ -110,7 +110,9 @@ class _SplashState extends State<Splash> {
final String? token = await userProvider.setAndGetToken(); final String? token = await userProvider.setAndGetToken();
if (token != null) { if (token != null) {
log(token); log(token);
context.read<MediaProvider>().getDownloadsList(); if (!kIsWeb) {
context.read<MediaProvider>().getDownloadsList();
}
RequestService.token = token; RequestService.token = token;
final result = await userProvider.getUserInfo(); final result = await userProvider.getUserInfo();
if (!result) { if (!result) {

View File

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