2016-11-19 37 views
0

什麼我目前在Firefox,但在Chrome不起作用。當我僅在Android上嘗試Chrome時,目前無法訪問桌面版Chrome(CentOS 7,它們不支持它),因此調試問題很困難。Chrome和服務人員推式API

這是我用它來檢查,如果推送通知支持:

function checkPushSupport() { 
    "use strict"; 
    if (!("serviceWorker" in navigator)) { 
     return false; 
    } 
    if (!("showNotification" in ServiceWorkerRegistration.prototype)) { 
     return false; 
    } 
    if (Notification.permission === "denied") { 
     return false; 
    } 
    if (!("PushManager" in window)) { 
     return false; 
    } 
    return true; 
} 

通過該測試,Android的瀏覽器支持它。

這是我如何測試一個瀏覽器已經被認購。它目前做了一個ajax調用,在某些時候將會改變爲使用IndexDB,但我仍然需要一次性找出IndexDB。

function checkSubscribe(endpoint) { 
    "use strict"; 
    var csrf; 
    var mid; 
    var n; 
    var postdata; 
    var endhash; 
    csrf = $("#container").attr("data-csrf"); 
    mid = $("#pushnotify").attr("data-mid"); 
    postdata = {csrf: csrf, mid: mid, endpoint: endpoint}; 
    $.ajax({ 
     type: "POST", 
     async: false, 
     url: "/ajax/checksubscribe.ajax.php", 
     data: postdata, 
     dataType: "json", 
     success: function (json) { 
      $("#container").attr("data-csrf", json.csrf); 
      endhash = json.endhash; 
      if (endhash.length === 40) { 
       $("#notify").attr("data-hash", endhash); 
      } 
      n = parseInt(json.result); 
      if (n > 0) { 
       swapButton(); 
      } 
      $("#notify").removeAttr("disabled"); 
      // attach action to button 
      $("#notify").bind("click", function() { 
       sendSubscribe(); 
      }); 
     } 
    }); 
} 

該函數在FireFox中可以正確工作。當它檢測到客戶端已經訂閱了該網站推,它使按鈕的網站(什麼用戶想要的通知)內訂閱和他們的端點的哈希創建按鈕上的數據散列屬性,哈希與ajax一起使用時,他們想要訂閱或取消訂閱功能。如果他們已經訂閱了頁面上的功能,它會將訂閱按鈕更改爲ubsubsribe按鈕。我不相信該功能在Chrome中失敗,但由於訂閱推送通知似乎不起作用,我不知道它是否正確檢測到推送訂閱。

這個我覺得是問題所在,那是多麼的用戶實際上是推訂閱的網站:

function addEndpoint(endpoint) { 
    "use strict"; 
    var csrf; 
    var postdata; 
    var endhash; 
    csrf = $("#container").attr("data-csrf"); 
    postdata = {csrf: csrf, endpoint: endpoint}; 
    $.ajax({ 
     type: "POST", 
     async: false, 
     url: "/ajax/notification.ajax.php", 
     data: postdata, 
     dataType: "json", 
     success: function (json) { 
      $("#container").attr("data-csrf", json.csrf); 
      endhash = json.endhash; 
      if (endhash.length === 40) { 
       $("#notify").attr("data-hash", endhash); 
      } 
     } 
    }); 
} 

function firstSubscription() { 
    "use strict"; 
    navigator.serviceWorker.ready.then(function (registration) { 
     return registration.pushManager.subscribe({userVisibleOnly: true}); 
    }).then(function (subscription) { 
     $("#notify").off("click"); 
     addEndpoint(subscription.endpoint); 
     sendSubscribe(); 
     $("#notify").bind("click", function() { 
      sendSubscribe(); 
     }); 
    }); 
} 

Android版Chrome瀏覽從不詢問用戶是否要訂閱,以及Ajax調用當他們確實想要訂閱的時候向我發送終端不會發生。當然,如果端點沒有創建,它不會,如果用戶從來不問,我想弄清楚爲什麼它工作得很好,在Firefox,但無法在Chrome中就不會發生。

一切都是HTTPS。

任何幫助表示讚賞,我試圖尋找在谷歌的文檔,但他們所有的教程似乎認爲正在使用谷歌的開發工具,和好了,我根本無法在CentOS使用它們。

回答

0

我唯一缺少的是manifest.json定義的gcm_sender_id - 瀏覽器(至少爲Android)似乎需要的是,即使是推式API規範的一部分。