Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package nilsnett.chinese;
- import android.content.Context;
- import android.content.Intent;
- import com.google.android.gcm.GCMBaseIntentService;
- import com.google.android.gcm.GCMRegistrar;
- import nilsnett.chinese.logic.Container;
- import nilsnett.chinese.logging.Mylog;
- public class GCMIntentService extends GCMBaseIntentService {
- private static final String SENDER_ID = "596267413333"; // part of URL when accessing https://code.google.com/apis/console/ for the project
- private boolean _isWaitingForAnswer;
- public GCMIntentService(String senderId) {
- super(senderId);
- log ("Constructed with ID "+ senderId);
- }
- public GCMIntentService() {
- super(SENDER_ID);
- log ("Constructed with default ID");
- }
- /** Registers this device for GCM messages */
- public void register(Context context) {
- if (_isWaitingForAnswer) return;
- GCMRegistrar.checkDevice(context);
- GCMRegistrar.checkManifest(context);
- String regId = GCMRegistrar.getRegistrationId(context);
- if (regId.equals("")) {
- log("RegID blank - calling register");
- _isWaitingForAnswer = true;
- GCMRegistrar.register(context, SENDER_ID);
- } else {
- log("Already registered", regId);
- Container.GcmRegistrationId = regId;
- }
- }
- @Override
- /** Deserializes the message and sends it to the GcmMessenger */
- protected void onMessage(Context context, Intent intent) {
- Mylog.i("onMessage()");
- String jsonString = intent.getStringExtra("object");
- String objectTypeString = intent.getStringExtra("type");
- Mylog.d("GCM Receive: Type: ["+objectTypeString+"] Data: ["+jsonString+"]");
- Class objectType = null;
- try {
- // Deserialize the object and send to the message handler
- objectType = Class.forName(objectTypeString);
- Object o = Container.Serializer.deserialize(objectType, jsonString);
- Container.GcmMessenger.send(objectType, o, context);
- } catch (ClassNotFoundException e) {
- Mylog.e("Class not found in GCM onMessage: " + objectTypeString);
- }
- }
- @Override
- protected void onError(Context context, String s) {
- Mylog.e("GCM registration failed! " + s);
- _isWaitingForAnswer = false;
- }
- @Override
- protected void onRegistered(Context context, String regId) {
- log("Registered", regId);
- Container.GcmRegistrationId = regId;
- _isWaitingForAnswer = false;
- }
- @Override
- protected void onUnregistered(Context context, String regId) {
- log("Unregistered", regId);
- _isWaitingForAnswer = false;
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- Mylog.v("CS GCMIntentService: Destroyed");
- _isWaitingForAnswer = false;
- }
- private void log(String string, String regId) {
- Mylog.v(String.format("CS GCMIntentService: %s . RegId Hash: %d\nGCMIntentService: %s", string, regId.hashCode(), regId));
- }
- private void log(String string) {
- Mylog.v("CS GCMIntentService: " + string);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement