pusher / push-notifications-web

Beams Browser notifications
MIT License
39 stars 19 forks source link

Beams client unable to clear device interests with beamsClient.clearDeviceInterests() #109

Closed TheAli711 closed 2 years ago

TheAli711 commented 2 years ago

Hi, I am starting a beams client when component loads in react, with the following code

window.navigator.serviceWorker.ready.then(serviceWorkerRegistration => {
                beamsClient = new PusherPushNotifications.Client({
                    instanceId: process.env.REACT_APP_BEAMS_INSTANCE_ID,
                    serviceWorkerRegistration
                });
                beamsClient
                    .start()
                    .then(beamsClient => beamsClient.getDeviceId())
                    .then(deviceId => console.log('Successfully registered with Beams. Device ID:', deviceId))
                    .then(() => beamsClient.addDeviceInterest(`App.Models.User.${user.data.id}`))
                    .then(() => beamsClient.getDeviceInterests())
                    .then(interests => console.log('Current interests:', interests))
                    .catch(console.error);
            });

But when the component unloads and I do

                     beamsClient
                .getDeviceInterests()
                .then(interests => console.log('Before Clearing:', interests))
                .then(() => beamsClient.clearDeviceInterests())
                .then(() => beamsClient.getDeviceInterests())
                .then(interests => console.log('After Clearing:', interests));

It does not clear the interests

Console Output

Successfully registered with Beams. Device ID: web-xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx
superAdminTopNav.js:49 Current interests: (3) ['App.Models.User.10', 'App.Models.User.13', 'App.Models.User.3']
superAdminTopNav.js:89 Before Clearing: (3) ['App.Models.User.10', 'App.Models.User.13', 'App.Models.User.3']
superAdminTopNav.js:92 After Clearing: (3) ['App.Models.User.10', 'App.Models.User.13', 'App.Models.User.3']
TheAli711 commented 2 years ago

Link to documentation: https://pusher.com/docs/beams/reference/web/#cleardeviceinterests

TheAli711 commented 2 years ago

So i opened a ticket and turns out I was querying fo device interests immediately after clearing, You have to give a delay (5 sec in my case, have not tried with less) for it to work as expected.