Beispiel #1
0
  @Override
  public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {

    boolean result = false;

    Log.v(TAG, "execute: action=" + action);

    if (REGISTER.equals(action)) {

      Log.v(TAG, "execute: data=" + data.toString());

      try {
        JSONObject jo = data.getJSONObject(0);

        gWebView = this.webView;
        Log.v(TAG, "execute: jo=" + jo.toString());

        gECB = (String) jo.get("ecb");
        gSenderID = (String) jo.get("senderID");

        Log.v(TAG, "execute: ECB=" + gECB + " senderID=" + gSenderID);

        GCMRegistrar.register(getApplicationContext(), gSenderID);
        result = true;
        callbackContext.success();
      } catch (JSONException e) {
        Log.e(TAG, "execute: Got JSON Exception " + e.getMessage());
        result = false;
        callbackContext.error(e.getMessage());
      }

      if (gCachedExtras != null) {
        Log.v(TAG, "sending cached extras");
        sendExtras(gCachedExtras);
        gCachedExtras = null;
      }

    } else if (UNREGISTER.equals(action)) {

      GCMRegistrar.unregister(getApplicationContext());

      Log.v(TAG, "UNREGISTER");
      result = true;
      callbackContext.success();
    } else {
      result = false;
      Log.e(TAG, "Invalid action : " + action);
      callbackContext.error("Invalid action : " + action);
    }

    return result;
  }
Beispiel #2
0
  public void onDestroy() {
    GCMRegistrar.onDestroy(getApplicationContext());
    gWebView = null;
    gECB = null;
    gForeground = false;

    super.onDestroy();
  }
Beispiel #3
0
 @Override
 public final void onReceive(Context context, Intent intent) {
   Log.v(TAG, "onReceive: " + intent.getAction());
   // do a one-time check if app is using a custom GCMBroadcastReceiver
   if (!mReceiverSet) {
     mReceiverSet = true;
     String myClass = getClass().getName();
     if (!myClass.equals(GCMBroadcastReceiver.class.getName())) {
       GCMRegistrar.setRetryReceiverClassName(myClass);
     }
   }
   String className = getGCMIntentServiceClassName(context);
   Log.v(TAG, "GCM IntentService class: " + className);
   // Delegates to the application-specific intent service.
   GCMBaseIntentService.runIntentInService(context, intent, className);
   setResult(Activity.RESULT_OK, null /* data */, null /* extra */);
 }
 private void handleRegistration(Context context, Intent intent)
 {
     String s = intent.getStringExtra("registration_id");
     String s1 = intent.getStringExtra("error");
     intent = intent.getStringExtra("unregistered");
     Log.d("GCMBaseIntentService", (new StringBuilder()).append("handleRegistration: registrationId = ").append(s).append(", error = ").append(s1).append(", unregistered = ").append(intent).toString());
     if (s != null)
     {
         GCMRegistrar.resetBackoff(context);
         GCMRegistrar.setRegistrationId(context, s);
         onRegistered(context, s);
     } else
     {
         if (intent != null)
         {
             GCMRegistrar.resetBackoff(context);
             onUnregistered(context, GCMRegistrar.clearRegistrationId(context));
             return;
         }
         Log.d("GCMBaseIntentService", (new StringBuilder()).append("Registration error: ").append(s1).toString());
         if ("SERVICE_NOT_AVAILABLE".equals(s1))
         {
             if (onRecoverableError(context, s1))
             {
                 int i = GCMRegistrar.getBackoff(context);
                 int j = i / 2 + sRandom.nextInt(i);
                 Log.d("GCMBaseIntentService", (new StringBuilder()).append("Scheduling registration retry, backoff = ").append(j).append(" (").append(i).append(")").toString());
                 intent = new Intent("com.google.android.gcm.intent.RETRY");
                 intent.putExtra("token", TOKEN);
                 intent = PendingIntent.getBroadcast(context, 0, intent, 0);
                 ((AlarmManager)context.getSystemService("alarm")).set(3, SystemClock.elapsedRealtime() + (long)j, intent);
                 if (i < MAX_BACKOFF_MS)
                 {
                     GCMRegistrar.setBackoff(context, i * 2);
                     return;
                 }
             } else
             {
                 Log.d("GCMBaseIntentService", "Not retrying failed operation");
                 return;
             }
         } else
         {
             onError(context, s1);
             return;
         }
     }
 }
Beispiel #5
0
  @Override
  public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {

    boolean result = false;

    Log.v(TAG, "execute: action=" + action);

    if (REGISTER.equals(action)) {

      Log.v(TAG, "execute: data=" + data.toString());

      try {
        JSONObject jo = data.getJSONObject(0);

        gWebView = this.webView;
        Log.v(TAG, "execute: jo=" + jo.toString());

        gECB = (String) jo.get("ecb");
        gSenderID = (String) jo.get("senderID");
        String gDeviceId = (String) jo.get("deviceID");
        String gDeviceIsReg = (String) jo.get("deviceIsReg");
        Log.v(TAG, "execute: ECB=" + gECB + " senderID=" + gSenderID);

        // 미등록 혹은 임시등록 이 아닌 경우  는 기등록이므로 레지스터 안함
        if (gDeviceId != null && !gDeviceId.equals("null") && !gDeviceId.equals("")) {
          if (gDeviceIsReg.equals("true")) {
            Log.v(TAG, "디바이스 푸쉬 아이디 기등록  = " + gDeviceId);
            callbackContext.success();
            return true; // 이미 등록된 디바이스 - 등록 중지
          }
        }
        Log.v(TAG, "디바이스 푸쉬 아이디 미등록 -> 등록시작");

        GCMRegistrar.register(getApplicationContext(), gSenderID);
        result = true;
        callbackContext.success();
      } catch (JSONException e) {
        Log.e(TAG, "execute: Got JSON Exception " + e.getMessage());
        result = false;
        callbackContext.error(e.getMessage());
      }

      if (gCachedExtras != null) {
        Log.v(TAG, "sending cached extras");
        sendExtras(gCachedExtras);
        gCachedExtras = null;
      }

    } else if (UNREGISTER.equals(action)) {

      GCMRegistrar.unregister(getApplicationContext());

      Log.v(TAG, "UNREGISTER");
      result = true;
      callbackContext.success();
    } else {
      result = false;
      Log.e(TAG, "Invalid action : " + action);
      callbackContext.error("Invalid action : " + action);
    }

    return result;
  }
  private void handleRegistration(final Context context, Intent intent) {
    GCMRegistrar.cancelAppPendingIntent();
    String registrationId = intent.getStringExtra(EXTRA_REGISTRATION_ID);
    String error = intent.getStringExtra(EXTRA_ERROR);
    String unregistered = intent.getStringExtra(EXTRA_UNREGISTERED);
    mLogger.log(
        Log.DEBUG,
        "handleRegistration: registrationId = %s, " + "error = %s, unregistered = %s",
        registrationId,
        error,
        unregistered);

    // registration succeeded
    if (registrationId != null) {
      GCMRegistrar.resetBackoff(context);
      GCMRegistrar.setRegistrationId(context, registrationId);
      onRegistered(context, registrationId);
      return;
    }

    // unregistration succeeded
    if (unregistered != null) {
      // Remember we are unregistered
      GCMRegistrar.resetBackoff(context);
      String oldRegistrationId = GCMRegistrar.clearRegistrationId(context);
      onUnregistered(context, oldRegistrationId);
      return;
    }

    // last operation (registration or unregistration) returned an error;
    // Registration failed
    if (ERROR_SERVICE_NOT_AVAILABLE.equals(error)) {
      boolean retry = onRecoverableError(context, error);
      if (retry) {
        int backoffTimeMs = GCMRegistrar.getBackoff(context);
        int nextAttempt = backoffTimeMs / 2 + sRandom.nextInt(backoffTimeMs);
        mLogger.log(
            Log.DEBUG,
            "Scheduling registration retry, backoff = %d (%d)",
            nextAttempt,
            backoffTimeMs);
        Intent retryIntent = new Intent(INTENT_FROM_GCM_LIBRARY_RETRY);
        retryIntent.setPackage(context.getPackageName());
        PendingIntent retryPendingIntent = PendingIntent.getBroadcast(context, 0, retryIntent, 0);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.set(
            AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + nextAttempt,
            retryPendingIntent);
        // Next retry should wait longer.
        if (backoffTimeMs < MAX_BACKOFF_MS) {
          GCMRegistrar.setBackoff(context, backoffTimeMs * 2);
        }
      } else {
        mLogger.log(Log.VERBOSE, "Not retrying failed operation");
      }
    } else {
      // Unrecoverable error, notify app
      onError(context, error);
    }
  }
  @Override
  public final void onHandleIntent(Intent intent) {

    System.out.println("Received intent " + intent);

    try {
      Context context = getApplicationContext();
      String action = intent.getAction();
      if (action.equals(INTENT_FROM_GCM_REGISTRATION_CALLBACK)) {
        GCMRegistrar.setRetryBroadcastReceiver(context);
        handleRegistration(context, intent);
      } else if (action.equals(INTENT_FROM_GCM_MESSAGE)) {
        // checks for special messages
        String messageType = intent.getStringExtra(EXTRA_SPECIAL_MESSAGE);
        if (messageType != null) {
          if (messageType.equals(VALUE_DELETED_MESSAGES)) {
            String sTotal = intent.getStringExtra(EXTRA_TOTAL_DELETED);
            if (sTotal != null) {
              try {
                int total = Integer.parseInt(sTotal);
                mLogger.log(
                    Log.VERBOSE, "Received notification for %d deleted" + "messages", total);
                onDeletedMessages(context, total);
              } catch (NumberFormatException e) {
                mLogger.log(
                    Log.ERROR, "GCM returned invalid " + "number of deleted messages (%d)", sTotal);
              }
            }
          } else {
            // application is not using the latest GCM library
            mLogger.log(Log.ERROR, "Received unknown special message: %s", messageType);
          }
        } else {
          onMessage(context, intent);
        }
      } else if (action.equals(INTENT_FROM_GCM_LIBRARY_RETRY)) {
        String packageOnIntent = intent.getPackage();
        if (packageOnIntent == null
            || !packageOnIntent.equals(getApplicationContext().getPackageName())) {
          mLogger.log(
              Log.ERROR, "Ignoring retry intent from another package (%s)", packageOnIntent);
          return;
        }
        // retry last call
        if (GCMRegistrar.isRegistered(context)) {
          GCMRegistrar.internalUnregister(context);
        } else {
          String[] senderIds = getSenderIds(context);
          GCMRegistrar.internalRegister(context, senderIds);
        }
      }
    } finally {
      // Release the power lock, so phone can get back to sleep.
      // The lock is reference-counted by default, so multiple
      // messages are ok.

      // If onMessage() needs to spawn a thread or do something else,
      // it should use its own lock.
      synchronized (LOCK) {
        // sanity check for null as this is a public method
        if (sWakeLock != null) {
          sWakeLock.release();
        } else {
          // should never happen during normal workflow
          mLogger.log(Log.ERROR, "Wakelock reference is null");
        }
      }
    }
  }
 private static String getName(String[] senderIds) {
   String flatSenderIds = GCMRegistrar.getFlatSenderIds(senderIds);
   return getName(flatSenderIds);
 }
 private static String getName(String as[])
 {
     return getName(GCMRegistrar.getFlatSenderIds(as));
 }
 {
     GCMRegistrar.internalUnregister(((Context) (obj)));
     continue; /* Loop/switch isn't completed */
 }