1

我目前正在玩推送通知。我可以让它在我的电脑上运行,但它不能在我的手机上运行!我尝试使用最新版本的 Firefox Nightly 和 Preview。

这是页面加载时我的前端 JavaScript 代码:

navigator.serviceWorker.register('service-worker.js').then(function(registration) {

            console.log('Before return')
            
            registration.pushManager.permissionState().then(function(state){
                console.log("State : " + state)
            })

            return registration.pushManager.getSubscription().then(async function(subscription) {

                if (subscription) {
                    console.log('Already have a subscription')

                    return subscription;
                }

                console.log('Getting vapid key')

                const response = await fetch('./subscription');
                const vapidPublicKey = JSON.parse(await response.text())['public_key']

                console.log(vapidPublicKey)

                const convertedVapidKey = urlBase64ToUint8Array(vapidPublicKey);

                console.log('Trying to subscribe')
                registration.pushManager.permissionState().then(function(state){
                    console.log("State juste before : " + state)
                })

                options = {
                    userVisibleOnly: true, //VisibleOnly: true,
                    applicationServerKey: convertedVapidKey
                }

                return registration.pushManager.subscribe(options).then(function(pushSubscription) {
                    console.log(pushSubscription.endpoint);
                }, function(error) {
                    console.log(error);
                });
            });
        }).then(function(subscription) {
            if (subscription){
                console.log('Fetching subscription')

                fetch('./subscription', {
                    method: 'post',
                    headers: {
                    'Content-type': 'application/json'
                    },
                    body: JSON.stringify({
                    subscription: subscription
                    }),
                });
            }
        });

如您所见,我在订阅之前记录了推送管理器的状态,但这是我的日志:

Before return
State : granted
Getting vapid key
 // vapid key 
Trying to subscribe 
State juste before : granted
DOMException: User denied permission to use the Push API.

我总是收到此异常,当被要求允许通知时,我单击允许,并在我的参数中允许此特定网站的通知。

我使用带有自签名证书的 https。我不明白为什么它说用户拒绝使用 Push API 的权限,我不知道该怎么做......

4

0 回答 0