I am stuck with this problem from 3 days and searched Google, but to no avail. I followed the instructions in the Push Notifications Example . But I get this nasty error when I try to implement it.
Uncaught (in promise) DOMException: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.
I realized that the colon (:) is not allowed in the Base64 String, but the server key specified in Firebase in the Cloud Messenging tab,
AAAA-8MWvk0:APA91bHmU8XL-rzOn9SPbpG98NSqAJcuhYBBh7oze_XhprBpK7Q9PPWm3PwBo6Llxby4zjhVtgvKPIaSAQGp-8RfMV10_d1GekzICrVX9oYO8pi6dOM4VTp52CCAzug6NYIa10tNddsgE2P5QowGAYcnRHxLkrHXsw
which contains a colon (don't worry, its just a test application, so no privacy issues).
When I tried to use the old server key, it just throws an error. I tried to use other keys given in Firebase too, but to no avail. Please tell me which server key to use and how?
I am attaching my piece of code that actually does push subscription.
const API_KEY = "AIzaSyByIOl-mW0pu8SEXFeutB8jq59hhiau0wI";
var GCM_ENDPOINT = 'https://fcm.googleapis.com/fcm/send';
const legacy = 'AIzaSyDGF8t125bJ4wBvYn_UdRewkTxHGr7KpH8';
const applicationServerPublicKey = 'AAAA-8MWvk0APA91bHmU8XL-rzOn9SPbpG98NSqAJcuhYBBh7oze_XhprBpK7Q9PPWm3PwBo6Llxby4zjhVtgvKPIaSAQGp-8RfMV10_d1GekzICrVX9oYO8pi6dOM4VTp52CCAzug6NYIa10tNddsgE2P5QowGAYcnRHxLkrHXsw';
function urlB64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
console.log(base64);
const rawData = window.atob(base64);
console.log(rawData);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
function endpointWorkaround(pushSubscription) {
if(pushSubscription.endpoint.indexOf('https://fcm.googleapis.com/fcm/send') !== 0) {
return pushSubscription.endpoint;
}
var mergedEndpoint = pushSubscription.endpoint;
if (pushSubscription.subscriptionId &&
pushSubscription.endpoint.indexOf(pushSubscription.subscriptionId) === -1) {
mergedEndpoint = pushSubscription.endpoint + '/' +
pushSubscription.subscriptionId;
}
return mergedEndpoint;
}
function sendSubscriptionToServer(subscription) {
console.log('TODO: Implement sendSubscriptionToServer()', JSON.stringify(subscription));
var mergedEndpoint = endpointWorkaround(subscription);
var temp = showCurlCommand(mergedEndpoint);
return temp;
}
function showCurlCommand(mergedEndpoint) {
if (mergedEndpoint.indexOf(GCM_ENDPOINT) !== 0) {
console.warn('This browser isn\'t currently ' + 'supported for this demo');
return;
}
var endpointSections = mergedEndpoint.split('/');
var subscriptionId = endpointSections[endpointSections.length - 1];
var curlCommand = 'curl --header "Authorization: key=' + API_KEY + '" --header Content-Type:"application/json" ' + GCM_ENDPOINT + ' -d "{\\"registration_ids\\":[\\"' + subscriptionId + '\\"]}"';
console.log(curlCommand);
return subscriptionId;
}
function initialiseState() {
if (!('showNotification' in ServiceWorkerRegistration.prototype)) {
console.warn('Notifications aren\'t supported.');
return;
}
if (Notification.permission === 'denied') {
console.warn('The user has blocked notifications.');
return;
}
if (!('PushManager' in window)) {
console.warn('Push messaging isn\'t supported.');
return;
}
var prom = new Promise(function(resolve, reject) {
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
serviceWorkerRegistration.pushManager.getSubscription().then(function(subscription) {
if (!subscription) {
subscribe();
return;
}
var temp = sendSubscriptionToServer(subscription);
if(temp){
resolve(temp);
}else{
reject("Oops!")
}
})
.catch(function(err) {
console.error('Error during getSubscription()', err);
reject(err);
});
});
});
return prom;
}
function unsubscribe() {
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
serviceWorkerRegistration.pushManager.getSubscription().then(
function(pushSubscription) {
if (!pushSubscription) {
return;
}
pushSubscription.unsubscribe().then(function() {
}).catch(function(e) {
console.log('Unsubscription error: ', e);
});
}).catch(function(e) {
console.error('Error thrown while unsubscribing from ' + 'push messaging.', e);
});
});
}
function subscribe() {
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey);
serviceWorkerRegistration.pushManager.subscribe({userVisibleOnly: true, applicationServerKey: applicationServerKey})
.then(function(subscription) {
console.log(subscription);
return sendSubscriptionToServer(subscription);
})
.catch(function(e) {
if (Notification.permission === 'denied') {
console.log('Permission for Notifications was denied');
} else {
console.log('Unable to subscribe to push.', e);
}
});
});
}
source
share