我编写代码来获取GCM id以发送chrome推送通知.我在下面写了代码来获取GCM id.
var ChromePushManager = function (serviceWorkerPath, callback) { if ('serviceWorker' in navigator) { navigator.serviceWorker.register(serviceWorkerPath).then(function (registration) { // Registration was successful console.log('ServiceWorker registration successful with scope: ', registration.scope); registration.pushManager.subscribe({userVisibleOnly: true}).then(function (subscription) { console.log("subscription.subscriptionId: ", subscription.subscriptionId); console.log("subscription.endpoint: ", subscription.endpoint); // TODO: Send the subscription subscription.endpoint // to your server and save it to send a push message // at a later date var register = ChromePushManager.getRegistrationId(subscription); callback(null, register); }); }).catch(function (err) { // registration failed :( console.log('ServiceWorker registration failed: ', err); }); } else { callback('Service workers aren\'t supported in this browser.', null); } }; ChromePushManager.getRegistrationId = function (pushSubscription) { if (pushSubscription.subscriptionId) { return pushSubscription.subscriptionId; } var endpoint = 'https://android.googleapis.com/gcm/send/'; parts = pushSubscription.endpoint.split(endpoint); if (parts.length > 1) { console.log("RegistrationId : " + parts[1]); return parts[1]; } } var chromePushManager = new ChromePushManager('../js/service-worker.js', function (error, registrationId) { if (error !== null) { console.log(error); } });
它在http:// localhost:8080中正常工作.我在服务器上传了这段代码.我试图在服务器中执行代码.我收到了错误.
ServiceWorker注册失败:DOMException:仅允许安全源(请参阅:https://goo.gl/Y0ZkNV).
我不知道这个错误说的是什么.你能解释一下这个错误意味着什么,并给我一个解决方案来解决我在服务器端的错误.
谢谢.
您需要在服务器上使用http s.
错误:仅允许安全的来源(请参阅:链接).
从错误链接: