コード例 #1
0
  @Override
  public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {
    boolean result;

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

    if (INITIALIZE.equals(action)) {
      try {
        context = getApplicationContext();
        saveConfiguration(context, data.getString(0));
        result = true;
      } catch (JSONException e) {
        Log.e(TAG, "execute: Got JSON Exception " + e.getMessage());
        result = false;
        callbackContext.error(e.getMessage());
      }

      clearNotificationQueue(context);
    } else if (REGISTER.equals(action)) {

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

      try {
        context = getApplicationContext();
        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);

        regid = getRegistrationId(getApplicationContext());

        if (regid.isEmpty()) {
          new AsyncRegister().execute(callbackContext);
        } else {
          sendJavascript(new JSONObject().put("event", "registered").put("regid", regid));
          callbackContext.success(regid);
        }
        result = true;
      } 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 (ARE_NOTIFICATIONS_ENABLED.equals(action)) {

      Log.v(TAG, "ARE_NOTIFICATIONS_ENABLED");
      final boolean registered = !getRegistrationId(getApplicationContext()).isEmpty();
      Log.d(TAG, "areNotificationsEnabled? " + registered);
      callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, registered));
      result = true;

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

      GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
      try {
        gcm.unregister();
        removeRegistrationId(getApplicationContext());
      } catch (IOException exception) {
        Log.d(TAG, "IOException!");
      }

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

    return result;
  }