0

Here is an issue I am having with Firebase and push notifications.

In a web app, I want a button which sends a remote notification when clicked. This notification is meant to be received by an iOS app working together with my web-app.

The present question is about how to make this work. The web offers some example of how to receive notifications in a web application, but I did not find much about sending one, and this is precisely what I need to do.

Below is the relevant code, the problem is to know how to write the code for the SendNotific() function, and maybe some other details. I hope someone, expert on the subject will be able to provide me with some advice.

<body>
<script>
  // Initialize Firebase.
  var config = {
    apiKey: "myyKeyyy",
    authDomain: "......firebaseapp.com",
    databaseURL: "https://......firebaseio.com",
    projectId: "....",
    storageBucket: "........appspot.com",
    messagingSenderId: "........."
  },
  app = firebase.initializeApp(config);
  db = firebase.firestore(app);
  const messaging = firebase.messaging();

function SendNotific() {
// Code to send a notification.
........
}
</script>

<input type='button' id='PushNotif' style='font-size:20px' value='Send notification!' onClick='SendNotific()'>
</body>
3
  • 1
    You can't send a message from a web client without also setting up your own server infrastructure to actually send the message. The web client will have to instruct the server to send the message. Commented Nov 29, 2018 at 14:47
  • I kind of expected that. And I can actually send a notification from the Firebase console indeed. But how do I make my web client instruct the server to send the message, as you suggest? Commented Nov 29, 2018 at 15:02
  • 1
    You should have a POST endpoint of the server which you can just trigger to let server know to handle the notification sending on your behalf and also you can instruct the server about the actual notification you want it to send in your POST request body Commented Nov 29, 2018 at 15:03

1 Answer 1

1

You cannot send notifications using client, for sending notifications firebase has an Admin SDK which should be used by the application server. Though if you are up to building a serverless app then you should consider Firebase Cloud functions which you can trigger using a HTTP endpoint and the cloud function can handle the notifications sending job.

Sign up to request clarification or add additional context in comments.

3 Comments

That seems the way to go indeed. I'll have see how to write a cloud function on Firebase then.
Yeah cloud functions are fun ! Also If this answer served your purpose then you should upvote it or accept it so it might help others as well ;)
I did start to follow that direction and made some progress. Unfortunately it does not quite work yet. I made another post about it (stackoverflow.com/questions/53552341/…).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.