4

我正在尝试在 KaiOS 应用程序中实现推送通知。我只是按照下面的链接。

关注所有链接后,推送在浏览器中有效,但在 KaiOS 应用程序中无效。如果有人有任何示例代码或文件,请分享。

任何帮助都会得到帮助。

4

2 回答 2

3

1)首先,在manifest.webapp

    "permissions": {
          "serviceWorker":{
             "description": "required for handle push."
          },
          "push":{
             "description": "New update push."
          },
          "desktop-notification": {
             "description": "New content update notification for the user."
          }
       }

2)服务工作者文件sw.js代码

self.addEventListener('push', function(event) {
    event.waitUntil(
        self.registration.showNotification('My Push', {
            body: 'Push Activated',
        })
    );
});

self.addEventListener('activate', e => {
    self.clients.claim();
});

3)在应用启动时添加服务工作者

registerSW : function() {
    if ('serviceWorker' in navigator) {
        navigator.serviceWorker.register('./sw.js').then(function(reg) {
            console.log('Service Worker Registered!', reg);
            reg.pushManager.getSubscription().then(function(sub) {
                if (sub === null) {

                } else {
                    console.log('Subscription object: ', sub);
                }
            });
        }).catch(function(e) {
            console.log('SW reg failed');
        });
    }
}

4) 通过任何 dom 元素(如按钮)调用 service worker

registerServiceWorker: function() {
    Notification.requestPermission().then(function(permission) {
        if (permission === 'granted') {
            if ('serviceWorker' in navigator) {
                navigator.serviceWorker.ready.then(function(reg) {

                    reg.pushManager.subscribe({
                        userVisibleOnly: true
                    }).then(function(sub) {
                        console.log('Endpoint URL: ', sub.endpoint);

                    }).catch(function(e) {
                        if (Notification.permission === 'denied') {
                            console.warn('Permission for notifications was denied');
                        } else {
                            console.error('Unable to subscribe to push', e);
                        }
                    });
                })
            }
        }
    });
}

而已。

于 2020-04-23T11:34:53.113 回答
1

我有同样的问题,但我遵循了这个简单的网络推送通知方法,

https://medium.com/@seladir/how-to-implement-web-push-notifications-in-your-node-react-app-9bed79b53f34

以及我解决了该问题,现在它可以正常工作。请不要忘记将以下权限添加到manifest.webapp文件中。

"permissions": {
    "serviceworker": {
        "description": "Needed for assocating service worker"
    },
    "desktop-notification": {
        "description": "Needed for creating system notifications."
    },
    "notifications": {},
    "push": {
        "description": "Required for being updated with new goals in soccer matches"
    },
    "geolocation": {
        "description": "Marking out user location"
    },
    "alarms": {
        "description": "Scheduling alarms"
    }
},

并且请参阅此 kaios 文档以在 kaios 设备上运行应用程序。

https://developer.kaiostech.com/getting-started/build-your-first-hosted-app/pwa-to-hosted-app

于 2020-04-23T05:13:14.553 回答