如何使用GCMReceiver和GcmListenerService时自定义通知显示和色调
我按照说明来设置一个Android GCM客户端应用程序这里,并专门有一个问题,此摘录:
I have followed instructions to set up an Android GCM client app here, and have a problem specifically with this excerpt:
对于延长WakefulBroadcastReceiver,谷歌现有的应用程序
建议迁移到GCMReceiver和GcmListenerService。至
迁移:在应用清单,以取代你GcmBroadcastReceiver
com.google.android.gms.gcm.GcmReceiver,并取代目前的
扩展IntentService新的服务宣言
GcmListenerService取下广播接收器实现
您的客户端code重构当前IntentService服务
实施使用GcmListenerService
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
这是我见过的GCM实现大多数例子中,使用扩展 WakefulBroadcastReceiver
A类。如这个。当你这样做,你有机会使用 NotificationManager
和自定义通知图标,声音等。但是,如果按照谷歌的建议,我不知道怎么样自定义通知。使用 GcmListenerService
由谷歌的建议大多数例子中,只是重写 onMessageReceived
方法。但该方法仅称为或者如果应用程序已经在前景接收到通知时,或者当用户点击通知本身。这种方法是不是可以自定义通知声音在正确的地方。声音被称为该方法之前已播放
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.
所以,如果我需要自定义通知声音,我也许应该覆盖 GcmListenerService
不同的方法,但没有资料显示哪一个。另一种选择是使用说明的声音
属性的此处。但你必须自己捆绑在应用程序的水库声音文件/原料
目录。这似乎是错误的。我宁愿只使用系统提供的声音,主题等。
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.
思考?
@SamStern 回答了这个问题对我来说,当我张贴在谷歌样品github上维基:
@SamStern answered this question for me, when I posted it on a Google Samples github wiki:
因此,有2种的GCM
邮件:
通知消息 - 这些是为了生成通知
与应用程序没有中间处理。他们只打
如果应用程序正在运行onMessageReceived。
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.
数据消息 - 这些都是旨在默默的数据传递给应用程序的
消息服务。他们打onMessageReceived,即使应用程序是
的背景。然后该服务可以选择生成通知
使用正常的系统通知的API,或者它可以选择处理
消息默默的。
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.
我的外卖是,如果一个客户端应用程序要自定义通知是如何presented给用户(即更改通知托盘中的图标,在应用程序的共享preferences播放声音取决于声音设置等等),那么我们必须使服务器发送数据电文,而不是通知邮件。 下面是在谷歌的样品项目的实施,展示如何处理数据信息。
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.