Ejemplo n.º 1
0
  @Override
  public boolean execute(String action, JSONArray data, CallbackContext callbackId) {
    Log.d("PushNotifications", "Plugin Called");

    // make sure the receivers are on
    registerReceivers();

    if (ON_DEVICE_READY.equals(action)) {
      checkMessage(cordova.getActivity().getIntent());
      return true;
    }

    if (REGISTER.equals(action)) {
      return internalRegister(data, callbackId);
    }

    if (UNREGISTER.equals(action)) {
      return internalUnregister(data, callbackId);
    }

    if (SET_TAGS.equals(action)) {
      return internalSendTags(data, callbackId);
    }

    if (SEND_LOCATION.equals(action)) {
      return internalSendLocation(data, callbackId);
    }

    if (START_GEO_PUSHES.equals(action)) {
      if (mPushManager == null) {
        return false;
      }

      mPushManager.startTrackingGeoPushes();
      return true;
    }

    if (STOP_GEO_PUSHES.equals(action)) {
      if (mPushManager == null) {
        return false;
      }

      mPushManager.stopTrackingGeoPushes();
      return true;
    }

    if (CREATE_LOCAL_NOTIFICATION.equals(action)) {
      JSONObject params = null;
      try {
        params = data.getJSONObject(0);
      } catch (JSONException e) {
        e.printStackTrace();
        return false;
      }

      try {
        // config params: {msg:"message", seconds:30, userData:"optional"}
        String message = params.getString("msg");
        Integer seconds = params.getInt("seconds");
        if (message == null || seconds == null) return false;

        String userData = params.getString("userData");

        Bundle extras = new Bundle();
        if (userData != null) extras.putString("u", userData);

        PushManager.scheduleLocalNotification(cordova.getActivity(), message, extras, seconds);
      } catch (JSONException e) {
        e.printStackTrace();
        return false;
      }

      return true;
    }

    if (CLEAR_LOCAL_NOTIFICATION.equals(action)) {
      PushManager.clearLocalNotifications(cordova.getActivity());
      return true;
    }

    if ("setMultiNotificationMode".equals(action)) {
      PushManager.setMultiNotificationMode(cordova.getActivity());
      return true;
    }

    if ("setSingleNotificationMode".equals(action)) {
      PushManager.setSimpleNotificationMode(cordova.getActivity());
      return true;
    }

    if ("setSoundType".equals(action)) {
      try {
        Integer type = (Integer) data.get(0);
        if (type == null) return false;

        PushManager.setSoundNotificationType(cordova.getActivity(), SoundType.fromInt(type));
      } catch (Exception e) {
        e.printStackTrace();
        return false;
      }

      return true;
    }

    if ("setVibrateType".equals(action)) {
      try {
        Integer type = (Integer) data.get(0);
        if (type == null) return false;

        PushManager.setVibrateNotificationType(cordova.getActivity(), VibrateType.fromInt(type));
      } catch (Exception e) {
        e.printStackTrace();
        return false;
      }

      return true;
    }

    if ("setLightScreenOnNotification".equals(action)) {
      try {
        boolean type = (boolean) data.getBoolean(0);
        PushManager.setLightScreenOnNotification(cordova.getActivity(), type);
      } catch (Exception e) {
        e.printStackTrace();
        return false;
      }

      return true;
    }

    if ("setEnableLED".equals(action)) {
      try {
        boolean type = (boolean) data.getBoolean(0);
        PushManager.setEnableLED(cordova.getActivity(), type);
      } catch (Exception e) {
        e.printStackTrace();
        return false;
      }

      return true;
    }

    if ("sendGoalAchieved".equals(action)) {
      JSONObject params = null;
      try {
        params = data.getJSONObject(0);
      } catch (JSONException e) {
        e.printStackTrace();
        return false;
      }

      try {
        // config params: {goal:"goalName", count:30}
        String goal = params.getString("goal");
        if (goal == null) return false;

        Integer count = null;
        if (params.has("count")) count = params.getInt("count");

        PushManager.sendGoalAchieved(cordova.getActivity(), goal, count);
      } catch (Exception e) {
        e.printStackTrace();
        return false;
      }

      return true;
    }

    Log.d("DirectoryListPlugin", "Invalid action : " + action + " passed");
    return false;
  }