How to customize notification display and tone when using GCMReceiver and GcmListenerService

后端 未结 2 573
北海茫月
北海茫月 2020-12-19 13:56

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 exten

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-19 14:28

    @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.

提交回复
热议问题