Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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
Javascript 只需轮询Web服务器即可发送浏览器推送通知_Javascript_Browser_Notifications_Push Notification_Service Worker - Fatal编程技术网

Javascript 只需轮询Web服务器即可发送浏览器推送通知

Javascript 只需轮询Web服务器即可发送浏览器推送通知,javascript,browser,notifications,push-notification,service-worker,Javascript,Browser,Notifications,Push Notification,Service Worker,即使页面未打开,我也需要创建浏览器推送消息。但我有一些局限性,因为我是在Salesforce中开发的。所以当加载页面时,我有一个javascript块,它调用后端方法并在回调函数中得到响应。我发现,从技术上讲,我可以使用serviceWorker,它可以通过postMessages与页面通信,并且可以显示通知。我已经找到了一个通过单击按钮显示通知的示例,但我仍然需要找到使用serviceWorker与页面通信以从服务器获取响应的方法。 我看过这本关于服务工作者的指南,但对我来说不是很清楚。我不想

即使页面未打开,我也需要创建浏览器推送消息。但我有一些局限性,因为我是在Salesforce中开发的。所以当加载页面时,我有一个javascript块,它调用后端方法并在回调函数中得到响应。我发现,从技术上讲,我可以使用serviceWorker,它可以通过postMessages与页面通信,并且可以显示通知。我已经找到了一个通过单击按钮显示通知的示例,但我仍然需要找到使用serviceWorker与页面通信以从服务器获取响应的方法。 我看过这本关于服务工作者的指南,但对我来说不是很清楚。我不想使用任何中间件作为GCM,只想在客户端使用JS。
请给我一些示例代码来做这件事。

不。。。。目前,只有一种方法可以在网站关闭时使用ServiceWorkerAPI向用户显示推送通知

第一种方式仅当您的网站处于运行状态时才会显示推送通知。 1) Ajax轮询 这种方法被Facebook称为comet方法。在这种方法中,您将定期轮询服务器(意味着您需要定期对服务器进行ajax调用),以检查用户是否有任何新的通知要显示。请检查下面的代码 $(文档).ready(函数(){ setInterval(updateCount,120000); }); var updateCount=函数(){ var url=example.com//给出您自己的url

    $.ajax({

        url:ajaxURl,
        type: "POST",
        dataType:"json",
        error: function(error){
                console.log("Error occured in the ajax call while saving the endpoint",error);
             },
        success:function(data){
          console.log(data)
          if(data == new notifcation){
          //then show notification 
          }
        },
        timeout:60000
    });
})

2) 服务工作者Api 在这种方法中,您需要在客户端浏览器中注册service worker api。服务器工作者api注册后,它将在浏览器的后台运行,并等待推送事件。 当推送事件触发时,即使您的用户未登录到您的网站,service worker api也会触发推送通知

Cons:- 1)the service worker api will only run in the certain browser 
       2)Your website should https for using the service worker api as they already mention in the article.

我希望我回答了你的问题。

你能澄清一下你的目标吗?如果您只想在页面打开时显示通知,则不需要服务人员。@Marco我需要在页面关闭时接收通知。这是一个很不幸的例子,当页面关闭时,如果不使用浏览器的推送服务,就无法接收通知。您提到的第一种方法是完全错误的@拉维·古普塔仔细阅读问题“页面未打开时”
Cons:- 1)the service worker api will only run in the certain browser 
       2)Your website should https for using the service worker api as they already mention in the article.