I have a self-hosted version of rocket.chat and I tried to slightly change it in both server and electron, and I faced a problem.
The change I made is inside the notification module, and a function called notify. When rocket.chat tries to send a desktop notification, should the client be an electron instance, I send an IPC message to the client to handle notification instead of showing the default browser Notification API.
Everything seems to be working, but sometimes for some reason, it resets to sending browser notification instead of the custom one. I couldn't pinpoint the problem, as this does not happen on development mode, but on the published .exe
file.
Something that I noticed recently is that it usually goes on that mode when it's staled (for example when you put your windows on sleep and open it the next day). Also when it was in that mode, I could not open the DevTools to see if there are some errors on the console (the app wouldn't respond to that action at all).
Here is how I am sending the notification to the client:
notify(notification) {
if (window.Notification && Notification.permission === 'granted') {
const message = {
rid: notification.payload != null ? notification.payload.rid : undefined,
msg: notification.text,
notification: true,
};
return onClientMessageReceived(message).then(function (message) {
if (window.RocketChatDesktop?.send) {
//process notification object for the electron version
window.RocketChatDesktop.send('show-notification', message)
} else {
//default browser notification API
}
});
}
},
Any idea what seems to be the problem?
window.RocketChatDesktop
is injected via the preload script, and in a packaged Electron app (especially after sleep/hibernation), the renderer can get suspended or restarted. When that happens,RocketChatDesktop
may be undefined, so your code falls back to the browser notification. A common fix is towait for the API to be ready
before sending, and handle system resume events, e.g., poll forwindow.RocketChatDesktop?.send
or log when it’s missing to avoid silent fallback.