How can my Android app react to a push notificatio

2019-06-14 02:55发布

问题:

I'm looking to make my app more responsive by having it automatically download some data (around 10KB) when it receives a notification. My two options (I think) are:

  1. Try and pack all the info I need into the 4k payload limit - this may be possible, but certainly wouldn't help with iOS/WP implementations, as they have much lower limits. It also would be pretty inextensible, and I'd need to hand-craft the messages.
  2. Send a notification which the app would react to and download data in the background.

If it's going to be 2, can I do that? Can it be done when the app isn't already running in the background or foreground?

For some updates I'd like to show a notification to the user. Can this be done in the same notification, or do I need to push another one?

Any similar info about iOS much appreciated, but not essential for answering the question!

回答1:

When using Google Cloud Messaging you create a broadcast receiver which receives the GCM push notification. The broadcast receiver can either handle the notification on its own or start an intent service (which is better suited for your logic that downloads data from the server, since it runs on a separate thread, and doesn't block the main GUI thread). You can display a notification and download data from your server as a result of the same notification.



回答2:

Look into BroadcastReceiver, once registered you can create a listener inside of your activity that will be called once the notification has been received.