Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Google Cloud Messaging (GCM) or Firebase? #27

Closed
MrDesjardins opened this issue Dec 27, 2016 · 4 comments
Closed

Google Cloud Messaging (GCM) or Firebase? #27

MrDesjardins opened this issue Dec 27, 2016 · 4 comments

Comments

@MrDesjardins
Copy link

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?

@gauntface
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants