GoogleChromeLabs / web-push-codelab

Other
557 stars 293 forks source link

Google Cloud Messaging (GCM) or Firebase? #27

Closed MrDesjardins closed 7 years ago

MrDesjardins commented 7 years ago

This repository explains Push Notification with Google Cloud Messaging (GCM) account but at this time, when trying to access the Google Cloud Messaging (GCM) account, the page refer to use Firebase. In fact, I couldn't find a way to get a API key that work with Google Cloud Messaging (GCM). My understanding is that it is deprecated. Is the documentation will be updated or we should just rely on the Firebase documentation from this point?

ramanand12 commented 7 years ago

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=="}}

gauntface commented 7 years ago

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

MrDesjardins commented 7 years ago

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.

gauntface commented 7 years ago

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.