After visiting Facebook website and closing its page, weeks after I'm still getting browser notifications from Facebook about messages, posts, etc.
How is it implemented? Could a website install some scripts to be executed indefinitely regardless of having the website opened in a browser?
The most of browsers are allowing what we called it Push notifications
For example if in Chrome Browser
if you look at chrome://settings/content/notifications
You will find all websites allowed to send you notifications, it's kind of embedded scripts that will be stored in the browser website's cache
The notifications are simply an implementation of Observer design Pattern
actually the website makes a socket connection with your browser so even if you close your browser you can get push notification!
Related
I have a strict requirement to only allow a logged in user to open my web app in one tab. I've noticed that WhatsApp and Google Messenger's web apps have implemented this. For example, trying to open those apps in more than one tab (be it on the same browser, different browser, or even different device) results in these warnings:
Anyone know how this is done? There must be some sort of sync happening between the server and the client to ensure that only one tab is open. But this would require a unique tab identifier, which can get quite complicated to build reliably. Anyone know how WhatsApp and Google Messenger are doing it? Their technique seems to work flawlessly.
I don't know exactly how this is done at WhatsApp and Google Messenger, but if you work with WebSockets (your post hast the tag "websocket", so I assume, you do) every tab has its single connection to the server, and if your users need to be logged in you could check if the user has already a open weboscket connection to your server.
Are push notification supposed to work only if the PWA is installed?
If I load the app in android chrome browser, push notifications are not displayed at all (background or having the site opened) even they come thru the wire.
If I install the app, all notifications are displayed properly (background or focused app).
Is this normal a behaviour or something wrong in the implementation?
This is as expected.
The service worker runs on a separate thread than the one used by your application. This is the reason why your web app can still receive and display notifications even if you (or your user) is not currently visiting the web site.
Therefore if the PWA is not installed (meaning the SW is not running on the client side), there is no code waiting for the incoming notifications.
I wrote an article about service workers, if you want to deepen the PWAs topic.
UPDATE
There is an article specifically from OneSignal about not receiving Push Notifications on Android, maybe you can find some hints. If you check the OneSignal dashboard, can you see your client registered there?
[This point is for other users landing to this question] If your browser does not show web notifications, you can verify on "Can I Use" web site that your browser version supports notifications and push API and eventually update it.
I noticed some news sites such as Washington Post are able to pop up Web Notifications even though I don't have the site open in a tab. I don't recall this being possible before. How is it even possible for a website to execute the JS necessary if the site isn't open? How does one accomplish this using Web Notifications, is there a particular setting to accomplish this?
I believe they are using Push Notifications via Service Workers.
You can check the current support status to see if it's fit for you. It's well supported in modern browsers (although perhaps not the full specification).
A service worker is a script that your browser runs in the background,
separate from a web page, opening the door to features that don't need
a web page or user interaction. Today, they already include features
like push notifications and background sync.
and
A service worker has a lifecycle that is completely separate from your
web page.
I have an issue where I'm asking my app to open a browser looking at my Server.
From that server page, The user can perform some stuff on an existing site, then redirects the user to a URL that the App catches and the App continues as normal.
The lifecycle looks like this:
App -> Web Site -> App
However, after the life cycle of the app and the app is closed, the webpage is still open in the browser (on the page where the redirect to the app occurred).
It's apparently difficult via javascript to close a tab which wasn't opened by javascript? (In my case, the tab was opened by the app, not by a different tab). Is there any way to get around this?
(I'm aware I could use a WebView, but WebViews are buggy, manufacturer specific, security holes that I want to avoid)
No, sorry. There is no standard Intent structure for this. While it is possible that some browsers offer something, I am not aware of any that do.
Bear in mind that there is no requirement that your ACTION_VIEW Intent open up a browser tab, as the user's chosen browser may not offer any sort of tab UI metaphor. check this reference old question
I have built a chrome extension in the past and I have used chrome notifications API in that extension so I'm familiar with how it works.
I recently found this website which shows this notifications when something happens but the weirdest part is that even when this website is not opened in any tab it pushes these notifications (as long as chrome is open). I want to know how they do it.
I checked my settings->extensions list to see if I installed their extension somehow but there is none. So where are they running this magic javascript from?
Website is called https://www.greentoe.com
According to the official documentation and as stated by #Andreas
The reason for this is that when a push message is received, the
browser can start up a service worker, which runs in the background
without a page being open, and dispatch an event so that you can
decide how to handle that push message.
The javascript code you are asking for is inside the worker.
If you want to see the workers actually used in your browser just go to chrome://inspect/#service-workers in a new tab. You can see the worker code by clicking on the inspect link under the worker name.