私のコードは同じです。fetch()を呼び出していますが、ネットワークとサーバーのリクエストにcookieのようなリクエストヘッダーがありません。このRequest headers not sent from Service Workerの投稿をもう1つ見つけましたが、問題を解決できませんでした。サービスワーカーjsのFetch()がリクエストヘッダを送信できませんでしたか?
self.addEventListener('push', function (event) {
event.waitUntil(
self.registration.pushManager.getSubscription().then(function (subscription) {
fetch('/user/get-notification', {
mode: 'no-cors',
method: 'post',
headers: {
'Authorization': 'Bearer ' + self.token,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(subscription)
}).then(function (response) {
if (response.status != 'success') {
console.log('Looks like there was a problem. Status Code: ' + response.status);
throw new Error();
}
return response.json().then(function (data) {
var title = data.title;
var message = data.message;
var icon = data.image;
return self.registration.showNotification(title, {
body: message,
icon: icon
});
});
}).catch(function (err) {
console.error('Unable to retrieve data', err);
});
})
);
});
私は同じ問題を抱えていますが、私は上記のコードでユーザー/取得通知の使用法は何も考えていません。 –
self.tokenはどういう意味ですか? –