I have added a notification to my chrome extension and I get the following error:
Unchecked runtime.lastError: The message port closed before a response was received.
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.type === 'RONotification') {
createNotification(request).then(() =>{
sendResponse({status: true});
});
} else {
if (functions[request.func]) {
if (!request.args) {
request.args = {};
}
request.args.sender = sender;
functions[request.func].call(this, request.args).then((response) => {
sendResponse(response);
});
}
}
return true;
});
function createNotification(request) {
return new Promise((resolve, reject) => {
try {
let notificationId = `notification-${Date.now()}`;
let notificationsOptions = createNotificationOptions(request);
chrome.notifications.create(notificationId, notificationsOptions, function (res) {
if (res) {
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
notificationWasDisplayed: true,
notificationId: notificationId
}, function (response) {
});
});
} else {
console.error("error create notification on background.js :", chrome.runtime.lastError);
}
});
}
catch (err) {
console.error("exception create notification on background.js :", chrome.runtime.lastError);
}
});
}
I see that the code lines which cause the errors are :
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
RONotificationWasDisplayed: true,
notificationId: notificationId
}, function (response) {
});
index.js:
function chromeOnChangeListener(request, sender, response) {
if (request.NotificationWasDisplayed === true) {
notifications.setBadgeCounter(notificationConstants.liNotificationKey).then(function (res) {
});
}
return true;
}
which is called on the cb from chrome.notification.create... How can I solve it?