Code Monkey home page Code Monkey logo

Comments (4)

ramanand12 avatar ramanand12 commented on June 23, 2024

How to send response to SERVER and get notification.... I need server side code.. please help

{"endpoint":"https://fcm.googleapis.com/fcm/send/c9JjXJNcVi8:APA91bE1icXXgMlctA9zqe9l-kcUK4n3tLJRfVVmoSNbXYmwIHJzDGsX34mi0IHb4eQWZ04g90gmdE4d4cN-1bVAIc01OwRjKLxAVDxhaHEA5qS4F6njWqs-A43Zs9grmvRPiVg0GdFw","keys":{"p256dh":"BMmspt2haV6kuI0VoyL4pTRA7SL2Tq1bB9bS0fZX0jUpMaSfw5pxYch8rJ_Wsw_7EYtJGezUhr99SunkVPpsykw=","auth":"8jOUgq2I9p8ilo2xUGMaKQ=="}}

from web-push-codelab.

gauntface avatar gauntface commented on June 23, 2024

Where are you seeing a reference to GCM?

https://developers.google.com/web/fundamentals/getting-started/codelabs/push-notifications/

The only reference is at the very end regarding how to deal with old / non-standard browsers.

Regarding server side code, are you tried looking at the web-push libraries referenced in the code lab?

Otherwise a full node demo can be found here: https://github.com/gauntface/web-push-book/tree/master/src/demos/node-server

from web-push-codelab.

MrDesjardins avatar MrDesjardins commented on June 23, 2024

Hello,

I am trying to cover the basic scenario of having one user to register on multiple devices and having this one to receive notification from the server. I got some success, but the documentation is lacking and often refer to Android (which I do not mind since I just want browser notification).

What I did is refered here for client side and here for server side.

The problem is it's working only with a single device since I am storing the token on the server which come from the last device registered by the user. The official multiple devices documentation for Web introduces multi device and topics (at the same time) and at some point just doesn't give any detail about how to subscribe/unsubscribe and send for multi device and partially cover topic. For example, this page doesn't even say to send a HTTP request "DELETE" to unsubscribe (from topic or device), I had to figure that out in an Android page.

Now, I am experimenting the use of TOPICS instead of the "device" to represent a user. On every device, the user subscribe to a topics/. And the server send a notification to topics/. So far it works but I cannot delete the message token in the scenario that the user do not want to user notification (I just successfully remove from his topic). I am using the code in this repository (the only example, undocumented) that use messaging.deleteToken(currentToken)which return me a successful promise, but the token is still there when requested messaging.getToken() which doesn't make sense.

My new server code with Topics to handle multiple user devices looks like this to register a new device:

public void RegisterTopic(string userIdentifierForAllDevices, string singleDeviceNoticationKey)
{
	var serverApiKey = ConfigurationManager.AppSettings["FirebaseServerKey"];
	var firebaseGoogleUrl = $"https://iid.googleapis.com/iid/v1/{singleDeviceNoticationKey}/rel/topics/{userIdentifierForAllDevices}";

	var httpClient = new WebClient();
	httpClient.Headers.Add("Content-Type", "application/json");
	httpClient.Headers.Add(HttpRequestHeader.Authorization, "key=" + serverApiKey);

	object data = new{};
	var json = JsonConvert.SerializeObject(data);
	Byte[] byteArray = Encoding.UTF8.GetBytes(json);
	var responsebytes = httpClient.UploadData(firebaseGoogleUrl, "POST", byteArray);
	string responsebody = Encoding.UTF8.GetString(responsebytes);
	dynamic responseObject = JsonConvert.DeserializeObject(responsebody);
}

To send a message, I am sending like I am doing in the first link I posted with a POST to https://fcm.googleapis.com/fcm/send. I found odd that I have 2 different URL (one with FCM and one with iid).

On the client side, I am doing as mentioned in this repository. Like I said, the problem is to unsubscribe as well as I found it awkward that I need to use topics for multiple devices.

from web-push-codelab.

gauntface avatar gauntface commented on June 23, 2024

It sounds like you are mixing up web push and Firebase Cloud Messaging.

Web Push is the web standard, Firebase Cloud Messaging does some magic behinds the scenes to return a firebase token that can be used with the Firebase Cloud Messaging API (NOT web push protocol).

This is the incorrect place to be asking for help, a better place would be to click the "Send Feedback" button on the top right of the documentation page you linked to.

That being said - Firebase Cloud Messaging will always return a token if you call getToken, this is by design. The expectation is that you simply don't send a message to a token if they unsubscribe OR you call deleteToken and save state that the user does not want to receive messages.

from web-push-codelab.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.