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

[android] onMessageReceived not being called when app not in foreground #515

Closed
AdriVanHoudt opened this issue Jan 15, 2016 · 28 comments
Closed

Comments

@AdriVanHoudt
Copy link
Contributor

The notification gets shown (because of default android handler I guess), and I can see the GCM log, but the plugin never catches it. It does work when I put the app back in the foreground so I guess some background registration is going wrong.
I have tried everything from any 1.4.x, 1.5.x version to downgrading my phonegap cli to 5.3.6 to going back in time with the code of my app.
Is there anything else I can try?

@fredgalvao
Copy link
Collaborator

Let me guess: you're sending your notification payload with all important fields (message, icon, style, etc) on the notification object instead of data?

@macdonst
Copy link
Member

@AdriVanHoudt yeah, I'm willing to be that @fredgalvao has come across the root cause of the problem. If you put everything in the notification section of the payload then the Android OS takes over and the our onMessageReceived is not called. The situation is described in this github issue:

google/gcm#63

@AdriVanHoudt
Copy link
Contributor Author

This appears to be fixing it thanks! Any reason why this was working before? Also @macdonst you put a milestone on this issue, can I ask why?

@fredgalvao
Copy link
Collaborator

You probably wasn't using the new GCM way of doing things (introduced a few versions ago on this plugin) when "things were working". That's my bet.

The milestone thing is for the docs, this topic has been a often-reported one and needs to be better documented.

@AdriVanHoudt
Copy link
Contributor Author

I was using version 1.4.2 at least and it failed on 1.4.5 so did a change happen there somewhere?

@macdonst
Copy link
Member

@AdriVanHoudt it seems like onMessageReceived is never called if the payload is using notification to send the information. It may be a change in Google Play Services. I need to get some time to research but I've been preparing for our PhoneGap Day conference next week.

@AdriVanHoudt
Copy link
Contributor Author

yeah weird, but it is fixed now for me so no hurry. I'll close this since it is fixed but we can still talk on on the why 😃

@macdonst
Copy link
Member

@AdriVanHoudt I'm going to re-open it as it will remind me document it for the next release.

@macdonst macdonst reopened this Jan 21, 2016
@AdriVanHoudt
Copy link
Contributor Author

Np 👍

On Thu, 21 Jan 2016 15:26 Simon MacDonald [email protected] wrote:

@AdriVanHoudt https://github.com/AdriVanHoudt I'm going to re-open it
as it will remind me document it for the next release.


Reply to this email directly or view it on GitHub
#515 (comment)
.

@StephJan
Copy link

I'll just add a little bit on this just to get a clear idea, because I have somewhat of a related problem (I guess I may have missed something, though).

On Android 5.x :

  • I send payloads having both notification and data filled, to cover both foregroung/background cases.
  • plugin is configured by default.

Upon push, OS takes hand, displays local notification, never passes hand to my handler defined as :

  push.on('notification', function (data) {
     console.log("MessagingService - notification : " + JSON.stringify(data));

    // popup implementation 
    AlertService.showMessage(data.additionalData.originator_name, JSON.stringify(data));
 });

This is never called, whether or not app is in foreground. Tapping the OS's notification does not bring front my app either. What am I doing wrong ?

(I would be perfectly happy if my payload was not long enough to be truncated by local notification)

Thanks

@fredgalvao
Copy link
Collaborator

What am I doing wrong ?

Not really wrong, but the answer to that question is:

I send payloads having both notification and data filled

As mentioned by @macdonst and on quite a few issues recently, if anything appears on the notification field, google/android will not delegate the handling of the notification to listeners, and will deal with it itself. And google doesn't know about your js events 😉. That's why we require all payload to be in the data field for android, so that the plugin can take over and do stuff.

All of this is documented in the GCM website, but we still need to mention it on our docs, hence the issues recently created.

@StephJan
Copy link

So basically it means that the following scenario is not possible ?

  • app in background: Android displays notification, when clicking on it app opens and event handler is triggered
  • app in foreground : app's event event takes care of everything.

I would be happy with Android taking care of everything all the time if the notification area could display more than one-line messages, but it doesn't :(

@AdriVanHoudt
Copy link
Contributor Author

if you send on data property and you can handle it however you want, when your js event is triggered you can see if the app was in back/foreground and react based on that

@StephJan
Copy link

If I only use data, doesn't this mean that when OS takes over when app is in background, it will create a native notification with pretty much anything in it, since I didn't properly setup notification section ?

@AdriVanHoudt
Copy link
Contributor Author

Afaik when you only use data, the os will pass it onto the plugin which will make the notification

@StephJan
Copy link

But of course this will only take place if app is foreground, so you won't be notified in realtime when app is background, right ?

@AdriVanHoudt
Copy link
Contributor Author

It will make the notification and the moment you click it it will open the
app and call the on('notification' handler

On Fri, 22 Jan 2016 10:36 StephJan [email protected] wrote:

But of course this will only take place if app is foreground, so you won't
be notified in realtime when app is background, right ?


Reply to this email directly or view it on GitHub
#515 (comment)
.

@StephJan
Copy link

I'll try tis then, thanks for the tip :)

For info, what will the notification consist of, since I don't provide any notification section to fill it properly ?

@AdriVanHoudt
Copy link
Contributor Author

depends on the keys, it is just the same stuff you would put into the notification property

@StephJan
Copy link

Never thought about trying that :)

I'll try that, thanks again for the heads up

@macdonst
Copy link
Member

@StephJan if you want the notification called when the app is in the background, see Background Notifications in the docs.

@StephJan
Copy link

@macdonst I actually followed suggestion from AdriVanHoudt, works like a charm:

  • everything in notification attribute, including attributes "title" and "message"
  • when app is foreground, OS doesn't do anything and forward to my handler
  • when app is background, OS displays notification, upon tap my app is brought foreground and handler called

@macdonst
Copy link
Member

@StephJan kk, as long as you are happy

@nuclearsagar
Copy link

@StephJan What happens when you receive 3 seperate notifications when your app is in background?
Do you receive all 3 notifications when your handler called?
Or just the last one?

@AdriVanHoudt
Copy link
Contributor Author

@nuclearsagar you should only recieve the notification that is being clicked on

@nuclearsagar
Copy link

@AdriVanHoudt Does it mean that I am loosing other 2 notifications?
Because, There is no point in clicking each and every notification that comes in drawer.
What should I do to get all these notifications in one go after opening an app.?

I am struggling at this issue since last 10 days.

@AdriVanHoudt
Copy link
Contributor Author

The other notifications stay in the schade(where you click them) how to get all notifications is beyond my knowledge

@lock
Copy link

lock bot commented Jun 5, 2018

This thread has been automatically locked.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

5 participants