Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GcmListenerService.onMessageReceived() is only called if app is in foreground #166

Closed
sailrish opened this issue Feb 12, 2016 · 6 comments
Closed
Assignees

Comments

@sailrish
Copy link

I can't find a way to customize a notification's sound. Ideally, I want to let my users select one of several system defined ringtones (see attached image below), just like Gmail does.

The onMessageReceived() method calls sendNotification() in order to customize the notification sound, icon, title and message that gets displayed (and the intent tied to it, describing the action to take when user clicks on the notification). However, this article suggests that GcmListenerService is used for "...automatically displaying simple notifications on the app’s behalf." . So why is the NotificationManager being used at all in this sample code?

In practice, I have noticed that onMessageReceived() is only called if the application is in foreground, when a push notification is received. And if the application is not running, something else in the GcmListenerService (and not onMessageReceived()) is responsible for displaying the notification to the user. Is this true? If so, it appears that there is no way to customize the formatting of the notification, if the application isn't running. For example, if I want to change the sound and icon of the displayed notification, the only way to do so is by specifying the appropriate attributes in the GCM payload described here. That's all fine and dandy, but it poses another challenge. What if I want to specify a sound to be played that is one of the system defined sounds (see image below)? The only way to do that is to bundle all these system sounds in my app's res/raw folder. But that seems silly. Every app will have to bundle the same set of sounds (shown in the image below) to accomplish this. What am I missing? It seems GCM is trying to be the "least common denominator" in order to interoperate with iOS devices. iOS does not allow customization of notification sounds, except if the app itself provides the sound files in its resource bundle. And GCM is going the same way with Android.

See my stackoverflow question here.

image

@samtstern
Copy link
Contributor

I think the real issue here is that you are only receiving onMessageReceived() in the foreground, when in fact (and in my experience) it should be called for all GCM notifications containing data.

Once you get that working, you can set up some UI like the screenshot above that allows the user to pick from a list of system notification sounds and save the URI of the sound to SharedPreferences or similar. Then on onMessageReceived you can get that value and set the notification sound.

@sailrish
Copy link
Author

Thanks for the quick reply @samtstern . I'll dig deeper to see why my onMessageReceived() isn't getting called. However, I'm curious what's causing the notifications to get displayed (and the sound to get played) even when it isn't getting called? In my class derived from GcmListenerService, I overrode onCreate() just as a test, and it was definitely getting called when a notification was received, even though onMessageReceived() wasn't called. And the notification was still displayed, and the sound played. So, the service is definitely starting up just fine in response to the notification.

@samtstern
Copy link
Contributor

So there are two kinds of GCM messages:

  • Notification Messages - these are intended to generate a notification with no intermediate processing by the application. They only hit onMessageReceived if the app is running.
  • Data Messages - these are intended to silently pass data to the app's messaging service. They hit onMessageReceived even if the app is in the background. The service may then choose to generate a notification using the normal system notification APIs, or it may choose to handle the message silently.

Is it possible that you are sending Notification messages and you want to be sending Data messages?

@sailrish
Copy link
Author

Ah got it! I'm indeed sending Notification Messages from my server. Looks like Notification Messages should only be used if all we want to do is display the notification in the notification tray. Then, data will be passed to the app, only once the user clicks on the notification, and there's a click_action defined in the GCM payload. But if the app wants to customize the notification to displayed in the notification tray, I should have my server send a Data Message instead, and let the app handle displaying of the icon, playing of the sound, etc? Now it is making sense.

@samtstern
Copy link
Contributor

Yep, that's the correct understanding. I'm going to close this issue now, good luck!

@sailrish
Copy link
Author

Sounds good, and thank you for your help. I have referenced this issue in my SO question.

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

No branches or pull requests

3 participants