One particular Firefox OS issue is that you can pass a path to an icon to use in the notification, but if the app is packaged you cannot use a relative path like /my_icon.png
. You also can't use window.location.origin + "/my_icon.png"
because window.location.origin
is null
in packaged apps.
The manifest origin field fixes this, but it is only available in Firefox OS 1.1+
.
See https://developer.mozilla.org/de/docs/Web/API/Notification/icon
However, one possible solution is to encode the image into an base64 url within packaged apps. You can receive base64
encoded images from an external resource and pass them directly to the Notification API too.
Example png encoder using canvas:
function createIcon(iconURL) {
return new Promise(function(resolve, reject) {
let canvas, ctx, image;
canvas = document.createElement('CANVAS');
ctx = canvas.getContext('2d');
image = new Image();
image.crossOrigin = 'Anonymous';
image.onload = function() {
let dataURL;
canvas.height = image.height;
canvas.width = image.width;
ctx.drawImage(image, 0, 0);
// Define the image format here more dynamically
dataURL = canvas.toDataURL('image/png');
// Resolve the promise
resolve(dataURL);
// Clear the memory
canvas = null;
};
image.onerror = function() {
reject(new Error('createIcon: Can not convert image to base64'));
};
// The image will load after setting an src attribute
image.src = iconURL;
});
}
Message:
let message = {
title: 'Batman message',
text: 'Robin pick my up at my cave'
};
Icon URL:
const iconURL = '/assets/icons/batman_icon128x128.png';
Usage example:
// Promise
createIcon(iconURL).then((dataURL) => {
// Gecko >= 22
if(typeof window.Notification === 'function') {
new Notification(message.title, {
body: message.text,
icon: dataURL
});
}
// Gecko < 22
else if (typeof navigator.mozSetMessageHandler === 'function') {
let notification = navigator.mozNotification.createNotification(
message.title,
message.text,
dataURL
);
notification.show();
}
}).catch(function(error) {
// Rejection
console.warn(error);
});