I have followed instructions to set up an Android GCM client app here, and have a problem specifically with this excerpt:
For existing apps that extend a WakefulBroadcastReceiver, Google recommends migrating to GCMReceiver and GcmListenerService. To migrate: In the app manifest, replace your GcmBroadcastReceiver with "com.google.android.gms.gcm.GcmReceiver", and replace the current service declaration that extends IntentService to the new GcmListenerService Remove the BroadcastReceiver implementation from your client code Refactor the current IntentService service implementation to use GcmListenerService
Most examples that I have seen for GCM implementation, use a class that extends WakefulBroadcastReceiver
. Such as this one. When you do that, you get the opportunity to use the NotificationManager
and customize the notification icon, sound, etc. However, if you follow Google's advice, I'm not sure how to customize the notifications. Most examples that use GcmListenerService
as suggested by Google, simply override the onMessageReceived
method. But that method is only called either if the application is already in the foreground when the notification is received, or when the user clicks on the notification itself. That method isn't the right place to customize the notification sound. The sound has already been played before that method is called.
So, if I need to customize the notification sound, I should probably override a different method in GcmListenerService
, but there's no documentation that shows which one. Another option is to use the sound
attribute described here. But then you have to bundle the sound files in the application's res/raw
directory yourself. That seems wrong. I'd rather just use the system provided sounds, themes, etc.
Thoughts?
@SamStern answered this question for me, when I posted it on a Google Samples github wiki:
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.
My takeaway is that if a client app wants to customize how a notification is presented to the user (i.e. change the icon in the notification tray, play a sound depending on sound settings in the app's shared preferences, etc), then we have to make the server send "Data Messages" instead of "Notification Messages". Here's the implementation in a Google Samples project, showing how to handle a Data Message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With