Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在GCM Service Worker中获取API请求后,如何使用JavaScript设置cookie?_Javascript_Django_Cookies_Google Cloud Messaging_Chrome Gcm - Fatal编程技术网

在GCM Service Worker中获取API请求后,如何使用JavaScript设置cookie?

在GCM Service Worker中获取API请求后,如何使用JavaScript设置cookie?,javascript,django,cookies,google-cloud-messaging,chrome-gcm,Javascript,Django,Cookies,Google Cloud Messaging,Chrome Gcm,我已经成功地实施了GCM,并且ServiceWorkers收到了推送通知 我的问题 无论何时有SOS更新,我都会向其发送一个HTTP请求,然后在Google Chrome上显示一个正常的通知,但没有任何有效负载或数据 据介绍,chrome无法接收有效负载/数据,因此在触发推送事件时,必须手动从后端获取通知 因此,为了获取数据,我在后端请求一个url(在django中)向我发送通知数据。但问题就在这里我如何知道必须从数据库/模型发送哪些通知的数据 注意:-我没有维护不同的数据库表/模型,以确定客户

我已经成功地实施了GCM,并且ServiceWorkers收到了推送通知

我的问题

无论何时有SOS更新,我都会向其发送一个HTTP请求,然后在Google Chrome上显示一个正常的通知,但没有任何有效负载或数据

据介绍,chrome无法接收有效负载/数据,因此在触发推送事件时,必须手动从后端获取通知

因此,为了获取数据,我在后端请求一个url(在django中)向我发送通知数据。但问题就在这里我如何知道必须从数据库/模型发送哪些通知的数据

注意:-我没有维护不同的
数据库表/模型
,以确定客户端读取的通知,因为它是一个SOS更新,不需要读取/未读取

无效的解决方法:-

我可以在客户端浏览器上设置cookie,在后端可以获得下一个通知(下面是代码)

但是,我返回JSON响应,因此我无法从服务器端设置cookie,因此我计划在客户端(即Javascript)上使用
数据['notification']['notif_id']
来设置cookie

MySW.js的代码片段如下所示:

self.addEventListener('push', function(event) {
  console.log('Push message', event);
  event.waitUntil(
    fetch("//localhost/get_sos_notification/").then(function(response){
        if(response.status!=200){
            console.log('Looks like there was a problem. Status Code: ' + response.status);
            throw new Error();
        }
        return response.json().then(function(data){
            if(data.error || !data.notification){
                console.error('The API returned an error.', data.error);
                throw new Error();
            }
            var title = data.notification.title;
            var message = data.notification.message;
            var icon = '//localhost/static/main/images/push-notification.png';
            var notificationTag = data.notification.tag;
            return self.registration.showNotification(title, {
              body: message,
              icon: icon,
              tag: notificationTag
            });
        });
    }).catch(function(err){
       console.log('Unable to retrieve data', err);
       var title = 'SOS - Update';
       var message = 'We were unable to get the information, please click here to know the real message.';
       var icon = '//localhost/static/main/images/push-notification.png';
       var notificationTag = 'notification-error';
       return self.registration.showNotification(title, {
           body: message,
           icon: icon,
           tag: notificationTag
       });
    })
  );
});
但是我无法在处理接收到的通知数据(如果成功接收)的位置执行
document.cookie=last\u notif\u id=data.notification.notif\u id
,因为JS脚本给出错误
无法检索数据引用错误:未在http://localhost/static/main/js/sw.js

其他信息:
//localhost/get\u sos\u notification/
是一个get请求,返回JSON,并映射到
类GetSOSNotification(视图)

我在谷歌上搜索了这么多,但没有找到任何解决办法

提前谢谢

self.addEventListener('push', function(event) {
  console.log('Push message', event);
  event.waitUntil(
    fetch("//localhost/get_sos_notification/").then(function(response){
        if(response.status!=200){
            console.log('Looks like there was a problem. Status Code: ' + response.status);
            throw new Error();
        }
        return response.json().then(function(data){
            if(data.error || !data.notification){
                console.error('The API returned an error.', data.error);
                throw new Error();
            }
            var title = data.notification.title;
            var message = data.notification.message;
            var icon = '//localhost/static/main/images/push-notification.png';
            var notificationTag = data.notification.tag;
            return self.registration.showNotification(title, {
              body: message,
              icon: icon,
              tag: notificationTag
            });
        });
    }).catch(function(err){
       console.log('Unable to retrieve data', err);
       var title = 'SOS - Update';
       var message = 'We were unable to get the information, please click here to know the real message.';
       var icon = '//localhost/static/main/images/push-notification.png';
       var notificationTag = 'notification-error';
       return self.registration.showNotification(title, {
           body: message,
           icon: icon,
           tag: notificationTag
       });
    })
  );
});