コード例 #1
0
 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;
         }
     }
 }
コード例 #2
0
  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);
    }
  }