Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i use firebase cloud messaging without notification permission? (Javascript)

Update: Google Bug Report Description

(as suggested by google dev advocate in comments on answer 1, filed a bug report; updating the content here since it more succinctly and precisely describes the problem)

I do not need or want to show any notifications to my user. And many users are not willing to give notifications permission because they assume they will start seeing notifications.

But I wish to push data to my web page from the server. The web page is active and in the foreground. This is the classic use case that Web Sockets were designed for.

I understand that I could write my own web socket server and somehow try to scale it, or go to some other third-party for an outsourced scalable web socket push solution.

But, isn't this is a very common "sub-use-case" of the messaging that Firebase Messaging is targeted towards? Therefore shouldn't Google support this use case? I can't see any fundamental technical show-stoppers, but since Google is so smart, please do enlighten me if I am missing something on why this cannot or should not be done.

Original StackOverflow Question Text:

I don't need background notifications or service workers. All I want is to send data to the web page when it is currently loaded and in the foreground.

Websockets do not need any permission but they need a websocket server and maintenance. It is difficult or expensive to scale it.

Firebase solves the problem fundamentally but I don't see why it must require a user to give notifications permission even though I only want to push data when the page is loaded; not in the background.

like image 380
user2297550 Avatar asked Nov 21 '18 00:11

user2297550


People also ask

Can I send push notifications without FCM?

How it is possible? It's definitely possible -- you don't have to use Firebase to deliver push notifications.

How do you handle notifications when an app is in the background in Firebase web?

Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you'll need to write code to handle the onMessageReceived callback.

How do I use Firebase Cloud Messaging in node JS?

Before any Firebase cloud messages can be sent using Node. js, an additional JSON file needs to be generated and installed on the server. This file contains a private key that allows Node. js code to communicate through the SDK to the Firebase messaging service and is generated from within the Firebase console.

How do I connect to Firebase Cloud Messaging?

Set up a JavaScript Firebase Cloud Messaging client app 1 Add Firebase to your JavaScript project. If you haven't already, add Firebase to your JavaScript project. ... 2 Configure Web Credentials with FCM. ... 3 Retrieve a messaging object 4 Configure Web credentials in your app. ... 5 Access the registration token. ...

How do I add push notifications to my Firebase project?

To subscribe your app to push notifications, you need to associate a pair of keys with your Firebase project. You can either generate a new key pair or import your existing key pair through the Firebase Console. Open the Cloud Messaging tab of the Firebase console Settings pane and scroll to the Web configuration section.

How do I create a push certificate in Firebase?

You can either generate a new key pair or import your existing key pair through the Firebase Console. Open the Cloud Messaging tab of the Firebase console Settings pane and scroll to the Web configuration section. In the Web Push certificates tab, click Generate Key Pair.

How to retrieve a token from a message in Firebase?

messaging.getToken () retrieves token by called firebase sdk,if token is found ,we send it to server for storage,else we ask for permission to user to send him notification by calling method RequestPermission (),which is custom function.


2 Answers

The problem is that Firebase Messaging is only using 1 method to deliver notifications. That is the Push API specification spec, and that specification (wrongly and unfortunately) does not allow a service worker to receive messages without the user allowing an unrelated permission to show notifications.

The fix would be for the Firebase Messaging team to provide a different way to deliver messages to active web pages -- long polling, or websockets.

But it would be extra work for them, and may be not enough people are requesting it.

like image 104
user2297550 Avatar answered Oct 17 '22 21:10

user2297550


It's to protect the user's preferences about what your app is allowed to do. The way push messaging works on browsers is by using a service worker. Even though you say you don't need a service worker, you are actually making using of it when using Firebase Cloud Messaging in your app.

Given that, the prompt is necessary because the browser doesn't know what you intend to do with that push message. If the user doesn't trust your app, they should have the right to limit what it can actually do, especially when they're not using your app. Mobile operating systems (iOS, Android) are the same way.

like image 41
Doug Stevenson Avatar answered Oct 17 '22 22:10

Doug Stevenson