简体   繁体   中英

Android push notification with Google Cloud Messaging and Google App Engine as backend

I want to push notification from GAE backend to Android application. I've successfully implemented communication from this link: https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/GcmEndpoints

But, I'm confused about logic here.

  1. In this example, we have executed client registration to GCM, but via AsyncTask. I want to notify users when some code finishes execution on GAE backend, but without need to call some AsyncTask from Android. All I need is to receive message from GAE when this code finishes it's execution. Is this possible? I just don't know how workflow should be here.
  2. Also, what needs to be done on client side, so that it will "listen" for new messages? Some service task which always runs in background? Or is this already done with public class GcmIntentService extends IntentService from this example, and I just need to identify message type?

If someone could explain this workflow to me, and some code examples would be nice too.

You should really go through the GCM guide , getting GCM running is not hard but it requires a series of steps that cannot be skipped.

In this example, we have executed client registration to GCM, but via AsyncTask. I want to notify users when some code finishes execution on GAE backend, but without need to call some AsyncTask from Android

A complete GCM implementation requires the client to register to GCM servers in order to get its unique ID and YOUR server to store that ID in order to contact that client directly, thats why you need at least one connection to send over this data to your server. you could get around this by using a new feature called topics , that way as soon as the client is registered it could suscribe to a topic and your server wouldn't need to know the specific GCM keys for any of your clients. I wouldn't recommend this last approach as it's not as scalable and will disable any 1 on 1 communication with your server.

Also, what needs to be done on client side, so that it will "listen" for new messages? Some service task which always runs in background? Or is this already done with public class GcmIntentService extends IntentService from this example, and I just need to identify message type?

You need to setup a broadcast reciever to handle incoming messages to your app (all detailed in the guide) from there you get the raw messages and you can do whatever you want with it, includng running it through an intent service as you state or processing it directly on the reciever (not recommended). Yes, you can discriminate message types and execute the logic accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM