Reputation: 916
What I have currently works in FireFox but does not work in Chrome. When I have only tried Chrome on Android, currently don't have access to Chrome on the Desktop (CentOS 7, they don't support it) so debugging issue is difficult.
This is what I use to check if push notifications are supported:
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;
}
By that test, Android Chrome supports it.
This is how I test if a browser is already subscribed. It currently does an ajax call, that will be changed to use IndexDB at some point but I still have to figure out IndexDB, one thing at a time.
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();
});
}
});
}
That function works accurately in FireFox. When it detects a client is already subscribed to push from the website, it enables the button to subscribe within the website (what the user wants notifications for) and creates a data-hash attribute on the button with a hash of their endpoint, that hash is used with ajax when they want to subscribe or unsubscribe from features. If they are already subscribed to the feature on the page, it changes the subscribe button to an ubsubsribe button. I do not believe that function is failing in Chrome but since subscribing to push notifications don't seem to work, I do not know if it properly detects a push subscription.
This I think is where the problem lies, it is how the user is actually push subscribed to the site:
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();
});
});
}
Chrome for Android never asks the user if they want to subscribe, and the ajax call to send me the endpoint when they do want to subscribe never happens. Of course it will not happen if the endpoint is not created which it won't if the user is never asked, I'm trying to figure out why it works just fine in FireFox but not in Chrome.
Everything is HTTPS.
Any help is appreciated, I tried looking at Google's documentation but all of their tutorials seem to assume that Google's Developer Tools are being used, and well, I simply can't use them on CentOS.
Upvotes: 0
Views: 188
Reputation: 916
The only thing I was missing was a gcm_sender_id
defined in manifest.json
- Chrome (at least for Android) seems to require that, even though it is not part of the Push API specification.
Upvotes: 0