我正在使用服务工作者进行推送通知.我正在使用XHR(Ajax)方法来获取我的通知,这是service-worker.js的代码片段:
var API_ENDPOINT = new Request('/getNotification', {
redirect: 'follow'});
event.waitUntil(
fetch(API_ENDPOINT, {credentials: 'include' })
.then(function(response) {
console.log(response);
if (response.status && response.status != 200) {
// Throw an error so the promise is rejected and catch() is executed
throw new Error('Invalid status code from API: ' +
response.status);
}
// Examine the text in the response
return response.json();
})
.then(function(data) {
console.log('API data: ', data);
var title = 'TEST';
var message = data['notifications'][0].text;
var icon = data['notifications'][0].img;
// Add this to …Run Code Online (Sandbox Code Playgroud)