Advertisement
Nizlor

CS GCMIntentService

Jun 30th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. package nilsnett.chinese;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import com.google.android.gcm.GCMBaseIntentService;
  6. import com.google.android.gcm.GCMRegistrar;
  7. import nilsnett.chinese.logic.Container;
  8. import nilsnett.chinese.logging.Mylog;
  9.  
  10.  
  11. public class GCMIntentService extends GCMBaseIntentService {
  12.     private static final String SENDER_ID = "596267413333";  // part of URL when accessing https://code.google.com/apis/console/ for the project
  13.     private boolean _isWaitingForAnswer;
  14.  
  15.     public GCMIntentService(String senderId) {
  16.         super(senderId);
  17.         log ("Constructed with ID "+ senderId);
  18.     }
  19.  
  20.     public GCMIntentService() {
  21.         super(SENDER_ID);
  22.         log ("Constructed with default ID");
  23.     }
  24.  
  25.     /** Registers this device for GCM messages */
  26.     public void register(Context context) {
  27.         if (_isWaitingForAnswer) return;
  28.         GCMRegistrar.checkDevice(context);
  29.         GCMRegistrar.checkManifest(context);
  30.         String regId = GCMRegistrar.getRegistrationId(context);
  31.         if (regId.equals("")) {
  32.             log("RegID blank - calling register");
  33.             _isWaitingForAnswer = true;
  34.             GCMRegistrar.register(context, SENDER_ID);
  35.         } else {
  36.             log("Already registered", regId);
  37.             Container.GcmRegistrationId = regId;
  38.         }
  39.     }
  40.    
  41.     @Override
  42.     /** Deserializes the message and sends it to the GcmMessenger */
  43.     protected void onMessage(Context context, Intent intent) {
  44.         Mylog.i("onMessage()");
  45.         String jsonString = intent.getStringExtra("object");
  46.         String objectTypeString = intent.getStringExtra("type");
  47.         Mylog.d("GCM Receive: Type: ["+objectTypeString+"] Data: ["+jsonString+"]");
  48.         Class objectType = null;
  49.         try {
  50.             // Deserialize the object and send to the message handler
  51.             objectType = Class.forName(objectTypeString);
  52.             Object o = Container.Serializer.deserialize(objectType, jsonString);
  53.             Container.GcmMessenger.send(objectType, o, context);
  54.  
  55.         } catch (ClassNotFoundException e) {
  56.             Mylog.e("Class not found in GCM onMessage: " + objectTypeString);
  57.         }
  58.     }
  59.  
  60.     @Override
  61.     protected void onError(Context context, String s) {
  62.         Mylog.e("GCM registration failed! " + s);
  63.         _isWaitingForAnswer = false;
  64.     }
  65.  
  66.     @Override
  67.     protected void onRegistered(Context context, String regId) {
  68.         log("Registered", regId);
  69.         Container.GcmRegistrationId = regId;
  70.         _isWaitingForAnswer = false;
  71.     }
  72.  
  73.     @Override
  74.     protected void onUnregistered(Context context, String regId) {
  75.         log("Unregistered", regId);
  76.         _isWaitingForAnswer = false;
  77.     }
  78.    
  79.     @Override
  80.     public void onDestroy() {
  81.         super.onDestroy();
  82.         Mylog.v("CS GCMIntentService: Destroyed");
  83.         _isWaitingForAnswer = false;
  84.     }
  85.  
  86.     private void log(String string, String regId) {
  87.         Mylog.v(String.format("CS GCMIntentService: %s . RegId Hash: %d\nGCMIntentService: %s", string, regId.hashCode(), regId));     
  88.     }  
  89.     private void log(String string) {
  90.         Mylog.v("CS GCMIntentService: " + string);     
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement