If a service worker is associated to a web page, does it inherit its permissions (for example to show notifications)? Could I use a service worker to create notification?
In general it seems that service worker inherits page permissions, and you can use a service worker to create notifications, if your page has received permissions for that. On the other hand not everything is available to the service worker API. For example, Geolocation access has been proposed, but is apparently not yet available. So you'll have to look at things case-by-case.
Notifications are indeed one of the best use cases for service worker, since your service worker, once registered, can actually create notifications even if your page is not open in the user's browser. Here is an tutorial for push notifications with service workers in Chrome. You could also create notifications normally without the whole push part though. Just keep in mind that your service worker needs to be woken up by some event or other in order to do anything.
Related
I am developing a webapp and currently my app server uses Server Sent Events to maintain a connection to the user to efficiently push things like new messages and other events to them without them having to constantly poll the server to ask.
I'm looking for a way to implement Push Notifications such that I can use the Notification Web API to deliver notifications to the user either when they are in a separate tab, or when they have it running on their phone and they have Chrome minimized.
After some research, it looks like Google Cloud Messaging has been replaced by Firebase Cloud Messaging, and this is the recommended service to use to deliver push notifications. It looks like it works by having the user maintain a persistent connection the Firebase Servers and your personal app server posts requests to Firebase for them to deliver to the user.
My question: does this alleviate the need to implement and maintain an SSE / WebSocket server? I'm wondering if I can't just forward all my events through FireBase instead and have them be delivered to the user via their service. That is, I would have two classes of messages both sent from Firebase:
One would be the typical "notifications" which are interpreted as such by the user (such as a new message), and require local permissions for the Notifications API. And then the other type would be other "real-time" updates that wouldn't require a notification (such as a message being edited, or the "user is typing a message" prompt)
Is this sort of thing possible / recommended, or is my understanding flawed in some aspect of this?
Nothing you're describing seems outside of capabilities of FCM.
It looks like it works by having the user maintain a persistent connection the Firebase Servers
Actually, it's not the "user". It's the messaging infrastructure provided by the device. For Android, the message get routed through Google Play services, which runs as a privileged process in the background. For iOS, messges go through Apple's APNS. These components maintain an open socket to their respective services, and they're more effective than the app managing the socket on its own, because the app can't manage a socket in the background indefinitely - the OS will shut it down after some time. This means the app can wake up and receive messages soon after they're sent.
I want to show the latest news of website in a notification on every browser when the website is opened or closed.
May I know how to implement the process? Which push api I need to use ? What is the procedure ?
In order to add push notifications to your site, you'll need to understand which technology/API is used for communicating between the client and server even the browser is closed. The Service Worker API allows this and you'll need to add a service worker to your site for push notifications to work.
Service workers essentially act as proxy servers that sit between web applications, the browser, and the network (when available). They are intended, among other things, to enable the creation of effective offline experiences, intercept network requests and take appropriate action based on whether the network is available, and update assets residing on the server. They will also allow access to push notifications and background sync APIs.
Source: MDN - Service Worker API
NOTE: In order for a Service Worker to work, your website needs to be secure and run on https:// For local development, this is not a requirement.
Once you have the Service Worker running, you'll need to register the user for accepting push notification. This allows the server to send messages to the client. Have a look at Push Notifications: What are they and how do I send them? to gain a better understanding of this process.
As this topic is quite brought and there are a lot of moving parts that you need to understand, I'd suggest to get familiar with Service Workers and Push Notifications through tutorials and articles. Here are a few free resources that I found helpful.
Service Workers - an Introduction
Offline Web applications
Service Workers: Push Notifications
Set Up a JavaScript Firebase Cloud Messaging Client App
I am really confused about the two technologies GCM and Service worker. We can use both for push notifications but the service worker are specifically for browser push notifications and GCM for mobiles. This is my theory about the above mentioned technologies. If i wrong then please share your valuable suggestions and give me a right guide.
Service Worker
A service worker is basically a special javascript environment, that runs in the background of browsers. Websites can register service workers and then they can run some code in the background.
GCM
Is a product of Google, which basically provides a server API for sending messages across devices. If you combine them with a service worker, you can send notifications to Chrome whenever you want (if the user allows you to register a worker).
I want to create a service worker which have a persistence connection with the server. I can't use GCM or any other third party to do that. i want to implement the server by my own.
When there is an update, Server should push a message and Service Worker should receive it, and display it as a notification. Even there is no relevant tab open it should display the message.
(I tried Eventsource and it is not suit for my requirement.)
And i want a solution in java.
can anybody give me a solution ?
In my mind it is not possible.
The browsers are implemented to trigger push event only when they receive a message from GCM for Chrome and the own Mozilla push system for Firefox.
Also you can't use websocket with service-worker.
Maybe you'll be able to do something with the Periodic synchronization when this feature will be available, but it's not really its purpose.
I need to call an insecure (http://) URL to fetch data form service worker file for GCM push notification for chrome. Is this possible anyway?
I did a long search and as #Jeff Posnick written in my another question's(how to register a service worker from different sub domain) answer. It seems for security reason it's not allowed by GCM team to call a insecure service in service worker or while working on GCM push notification for chrome.
Not to reboot this thread, but I stumbled upon this as well. Two solutions are creating a github repository (where you have a standard ssl-certificate) or getting a free certificate from startssl. For the last one, you might need permissions that are not standard in your hosting package.