From 28769cb5f8c5c82e03ed7bec5197c38825e42610 Mon Sep 17 00:00:00 2001 From: Cali93 <32299095+Cali93@users.noreply.github.com> Date: Wed, 6 Sep 2023 22:01:33 +0300 Subject: [PATCH] fix(notification): fix background notification condition --- dapps/web3inbox/index.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/dapps/web3inbox/index.js b/dapps/web3inbox/index.js index 4587e9ae..7f1d7a93 100644 --- a/dapps/web3inbox/index.js +++ b/dapps/web3inbox/index.js @@ -56,30 +56,28 @@ async function registerClient(deviceToken, clientId) { .catch(error => console.log('error', error)); } +PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS); + messaging() .getToken() .then(async token => { - console.log({token}); - const authStatus = await messaging().requestPermission(); + const authStatus = await messaging().requestPermission( + PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS, + ); const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL; console.log({enabled}); - if (!enabled) { - PermissionsAndroid.request( - PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS, - ); + if (enabled) { + notifyClient = await NotifyClient.init({ + core, + projectId, + relayUrl, + }); + const clientId = await notifyClient.core.crypto.getClientId(); + await registerClient(token, clientId); } - messaging().setAutoInitEnabled(true); - - notifyClient = await NotifyClient.init({ - core, - projectId, - relayUrl, - }); - const clientId = await notifyClient.core.crypto.getClientId(); - await registerClient(token, clientId); }); messaging().setBackgroundMessageHandler(async remoteMessage => { @@ -90,7 +88,7 @@ messaging().setBackgroundMessageHandler(async remoteMessage => { relayUrl, }); } - if (!remoteMessage.data?.blob || remoteMessage.data?.topic) { + if (!remoteMessage.data?.blob || !remoteMessage.data?.topic) { console.log('Missing blob or topic on notification message.'); return; }