ToothlessGear / node-gcm

A NodeJS wrapper library port to send data to Android devices via Google Cloud Messaging
https://github.com/ToothlessGear/node-gcm
Other
1.3k stars 208 forks source link

Android only fails to receive gcm message #202

Closed akyoto closed 8 years ago

akyoto commented 8 years ago

Hi,

I'm using node-gcm for sending push notifications to users who add registrationTokens via the browser with service workers and those tokens are saved on a single server where the push notifications are being sent from. This server is whitelisted in the Google Project and uses a server API key.

On my desktop everything works fine. The notification arrives, everything perfect.

Now when I take my Android phone and "subscribe" (adding a new registration token) to notifications the subscription itself works flawlessly. I double-checked my database and it has 2 device tokens registered (Desktop + Android mobile phone). Now when I send a notification with 2 registration tokens using the .send() function this happens:

{ multicast_id: 8094808689750894000,
  success: 2,
  failure: 1,
  canonical_ids: 0,
  results: 
   [ { message_id: '0:1452909642961861%0f493ae60f493ae6' },
     { error: 'MismatchSenderId' },
     { message_id: '0:1452909642961862%d8fb87f4d8fb87f4' } ] }

Notifications seem to never work on Android. I can't seem to figure out the reason for MismatchSenderId. I tried googling this error message but the solutions don't apply to me (my server key is correctly set up and my manifest.json includes the sender ID). Some solutions also say that a recompile and reinstall of the Android app worked for them, however, again this doesn't apply in my case because I'm not using an Android app. I'm registering/subscribing devices via the browser. Tried clearing all sorts of caches to see if that helps, no success.

The message only includes a notification:

let message = new gcm.Message({
    notification
})

Including test data doesn't have any effect.

Any idea what's wrong here?

eladnava commented 8 years ago

@blitzprog Could this be the case? I know you mentioned having only one API key and sender ID, but MismatchSenderId is definitely a result of mixing up sender IDs and API keys.

I believe that MismatchSenderId means that you are trying to send push notifications using a GCM Server API Key that is linked to a different SENDER_ID than was used to generate some of your registration IDs on the client-side.

https://github.com/ToothlessGear/node-gcm/issues/162#issuecomment-145384668

akyoto commented 8 years ago

I only have 1 sender ID (defined in manifest.json as gcm_sender_id and it's the project number). I also only use 1 server key. The push notifications using the very same site, same service worker, fail on Android Chrome registration tokens but work with Desktop Chrome generated tokens.

eladnava commented 8 years ago

This may be a shot in the dark, but try creating a brand new project in the Google APIs Console, generating a new API key, and re-registering devices with the new SENDER_ID. (Project ID)

https://community.pushwoosh.com/questions/149/what-could-cause-mismatchsenderid-on-some-android-devices/193

akyoto commented 8 years ago

Ouch. That would invalidate all the device tokens currently registered, right? Well, I guess it can't be helped. Going to try it and let you know if it worked.

akyoto commented 8 years ago

A little update: I'm sorry for naming the wrong error source. MismatchSenderId is completely irrelevant to the problem that the notification is not shown on Android. I noticed I had one excessive token in the database.

Now I have 2 "successful" messages but still only the desktop one actually appears on screen.

akyoto commented 8 years ago

I created a completely new project with a new sender ID and new server key, it didn't have any effect. Desktop notification arrives, Android one doesn't.

I also tested manually triggering the "push" event on the device via chrome://serviceworker-internals/. The notification was shown correctly. That means my service worker code is not faulty.

The big question is: If node-gcm doesn't complain and the results are also labeled as "success" (I guess that's coming from Google servers) why is the push event itself not triggering?

eladnava commented 8 years ago

@blitzprog If the results are labled "success" now, then it seems like GCM is consuming the push notification for your Android device and not issuing it correctly, either on purpose or due to a bug.

You can try manually issuing the GCM push request (using Postman for example) to isolate the cause and remove node-gcm from the equation.

akyoto commented 8 years ago

@eladnava: Thank you for the detailed help even though this seems to be a mistake on Google's side and not the node-gcm package itself. I tried settings lots of things, played around with message options but in the end it just doesn't work on Android.

This guy has the same problem and it was written ~8 hours ago. http://stackoverflow.com/questions/34824859/success1-still-no-notification-in-android

I am assuming this is a bug in Google's server code so we can close this issue for now.

eladnava commented 8 years ago

Interesting. Definitely looks like an issue on Google's side. Hope it gets resolved quickly!

hypesystem commented 8 years ago

Hey @blitzprog -- how are you developing your Android app? We have seen a lot of issues with development environments other than the Android SDK (like phonegap, etc). One problem has been lacking support for the notification field.

It might just be Google's services being slow, though. They have no guarantee for delivery whatsoever, so notifications can be very delayed (or even dropped).

akyoto commented 8 years ago

As I mentioned in the first post, it's not an Android app but a standard website which registers a service worker to receive push notifications.

The whole push workflow is working (I confirmed it by triggering the push event manually in the browser and it showed my notification). The problem is that the push event itself is not triggered in the service worker by GCM. I can't explain this behaviour, therefore I assume it must be a bug.

Additionally, Facebook's notifications also stopped working on my phone (Nexus 5) even though they worked on my phone in the past and they still work on Desktop.

Other devices are also affected because I asked some friends to test the push notifications with their phone and they confirmed that it doesn't work.

akyoto commented 8 years ago

I've asked about this behaviour on the android-gcm Google Group but I haven't gotten a response yet: https://groups.google.com/forum/#!topic/android-gcm/wtHaMktu1qA

hypesystem commented 8 years ago

Have I understood it correctly if I understand it as an issue with Chrome on Android specifically?

akyoto commented 8 years ago

@hypesystem I don't know how it works under the hood (whether it's a Chrome on Android bug or GCM on Android bug). I reckon I should ask on the chrome board as well.

hypesystem commented 8 years ago

I can't find anything on whether GCM notifications are supported on websites in Chrome on Android (only links talk about desktop Chrome as far as I can tell). Do you have any resources that indicate that Chrome on Android would be supported, too?

akyoto commented 8 years ago

@hypesystem: I've linked it in the first post. It also shows a gif:

Android Chrome Push

The Facebook notifications I was talking about: http://www.theverge.com/2015/9/15/9328915/chrome-facebook-push-notifications-android

Edit: I just tried the demo on my phone and it worked. Weird.

hypesystem commented 8 years ago

Ah, my bad, I didn't notice that original link.

It seems that your website is different from the example. We are entering territory where I am not well equipped to help you, but please keep us updated if you figure out what is wrong :smile:

akyoto commented 8 years ago

Hi @hypesystem, I'm replying because you asked me to keep you up to date.

Firefox 44 was just released which has its own endpoint. I couldn't get it to work with node-gcm (overwriting uri).

I switched to web-push which was specifically made for my use case and the result is:

Sorry to say that I can't use your library. web-push solved 2 problems at once for me.

hypesystem commented 8 years ago

Alright, glad you solved your issue :smile:

@eladnava this is a use case we have never worked at supporting, but it would be interesting to see if we could easily cover the use case that web-push covers, or if it is too different from normal notifications :smile:

eladnava commented 8 years ago

@hypesystem as per the outcome of https://github.com/ToothlessGear/node-gcm/issues/205#issuecomment-178397812, this is something we will not be implementing as it is out of the scope of the package.

hypesystem commented 8 years ago

:+1: