0

이것은 웹 푸시 모듈을 사용하여 알림을 보내는 내 server.js 코드입니다.푸시 알림이 크롬으로 전송되지 않습니다 (데스크톱 수준)

self.addEventListener('push', function (event) { 
    var payload = event.data ? event.data.text() : 'No Text Body'; 
    console.log(payload,'payload'); 
    event.waitUntil(
    self.registration.showNotification('Notify Message', { 
     lang: 'la', 
     body: 'You have subscribed to Pushnotification.', 
     icon: 'https://pushover.net/images/icon-256.png', 
     requireInteraction: true 
    }) 
); 
}); 

이를 :

endpoint = subscription.endpoint; 
var rawKey = subscription.getKey ? subscription.getKey('p256dh') : ''; 
key = rawKey ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) : ''; 
var rawAuthSecret = subscription.getKey ? subscription.getKey('auth') : ''; 
authSecret = rawAuthSecret ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawAuthSecret))) : ''; 

이 알림을 수신하는 서비스 노동자 코드 :

exports.sendWelcomePushNotification = function (req, res) { 
    var details = { 
     endpoint: req.body.endpoint, 
     key: req.body.key, 
     secret: req.body.authSecret 
    } 
    webPush.sendNotification(details.endpoint, {TTL: 0, payload: 'You have subscribed to Pushnotification.', userPublicKey: details.key, userAuth: details.secret}).then(function() { 
     res.sendStatus(200).json({message: 'User successfully subscribed'}); 
    }); 
}; 

이 브라우저 클라이언트 측 코드

는 인증 및 키 엔드 포인트를 캡처 코드는 firefox.ie에 대해 잘 작동합니다. 알림을 허용하면 API 요청이 전송되고 웹 푸시는 엔드 포인트에 푸시 알림을 보내고 환영 푸시 알림을받을 수있게됩니다. 그러나 크롬에서 같은 코드가 작동하지 않습니다. 즉; 어떤 오류도주지 않는 동시에 환영 푸시 알림을 제공하지 않습니다.

누군가가 도와 줄 수 있습니까? 어떤 종류의 도움이라도 대단히 감사하겠습니다. 감사합니다. .

+0

이가 자주 GCM API를 키 (매니페스트에 선언) 당신의 보낸 사람 ID 사이의 불일치로 인해 발생합니다. 이를 확인한 다음 브라우저에서 구독을 취소하고 다시 구독하여 알림을 푸십시오. – collimarco

+0

프로젝트 번호를 보낸 사람 ID로, 서버 키를 GCM API 키로 지정했습니다. –

+0

SW 기록을 보셨습니까? 크롬에 뭐라도 나니? – Jay

답변

0

크롬의 경우 GCM API 키 (https://github.com/web-push-libs/web-push#setgcmapikeyapikey)를 설정하거나 VAPID (https://github.com/web-push-libs/web-push#using-vapid-key-for-applicationserverkey)를 사용해야합니다.

오페라는 VAPID를 지원하지 않지만 GCM과 작동하기 때문에 둘 다 지원할 것을 제안합니다.

+0

예. 이미 GCM API 키를 설정했습니다. –

+0

gcm_sender_id와 GCM API 키가 동일합니까? –

+0

아니요, 두 값이 다릅니다. 발신자 ID는 앱 매니페스트에 입력되는 값입니다. – Marco

0

웹 푸시를 사용하는 경우 암호화 된 데이터로 할 일이있을 수 있습니다. server.js의 아래 코드를 사용해보십시오 :

var webPush = require('web-push'); 

const VALID_SUBSCRIPTION = { 
    endpoint: 'your-endpoint-id', 
    keys: { 
    p256dh: 'userPublicKey', 
    auth: 'your-auth-secret' 
} 
}; 

var payload = { 
    title: "This is a title", 
    body: "this is the body", 
    icon: "images/someImageInPath.png" 
} 

webPush.setGCMAPIKey("your-gcmapikey"); 

webPush.sendNotification(VALID_SUBSCRIPTION.endpoint, { 
    TTL: 10, 
    payload: JSON.stringify(payload), 
    userPublicKey: VALID_SUBSCRIPTION.keys.p256dh, 
    userAuth: VALID_SUBSCRIPTION.keys.auth, 
    }) 
    .then(function() { 
    console.log(JSON.stringify(payload)); 
    }); 
+0

VALID_SUBSCRIPTION.keys.p256dh, VALID_SUBSCRIPTION.keys.auth는 가입 키이고 auth 값은 맞습니까? 웹 푸시에서 동일한 값을 부여했습니다. –