/** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    // NetworkUtils.useSSL = true;

    // Register receivers for push notifications
    registerReceivers();

    // Create and start push manager
    PushManager pushManager = new PushManager(this, APP_ID, SENDER_ID);
    pushManager.onStartup(this);

    // The commented code below shows how to use geo pushes
    // pushManager.startTrackingGeoPushes();

    // The commented code below shows how to use local notifications
    // PushManager.clearLocalNotifications(this);

    // easy way
    // PushManager.scheduleLocalNotification(this, "Your pumpkins are ready!", 30);

    // expert mode
    // Bundle extras = new Bundle();
    // extras.putString("b", "https://cp.pushwoosh.com/img/arello-logo.png");
    // extras.putString("u", "50");
    // PushManager.scheduleLocalNotification(this, "Your pumpkins are ready!", extras, 30);

    mGeneralStatus = (TextView) findViewById(R.id.general_status);
    mTagsStatus = (TextView) findViewById(R.id.status);
    mIntTags = (EditText) findViewById(R.id.tag_int);
    mStringTags = (EditText) findViewById(R.id.tag_string);

    checkMessage(getIntent());

    mSubmitTagsButton = (Button) findViewById(R.id.submit_tags);
    mSubmitTagsButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            // PushManager.getTagsAsync(MainActivity.this, tagsListener);
            checkAndSendTagsIfWeCan();
          }
        });

    SendTagsFragment sendTagsFragment = getSendTagsFragment();
    mTagsStatus.setText(sendTagsFragment.getSendTagsStatus());
    mSubmitTagsButton.setEnabled(sendTagsFragment.canSendTags());
  }
  private boolean internalSendLocation(JSONArray data, CallbackContext callbackContext) {
    if (mPushManager == null) {
      return false;
    }

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

    double lat = 0;
    double lon = 0;

    try {
      lat = params.getDouble("lat");
      lon = params.getDouble("lon");
    } catch (JSONException e) {
      e.printStackTrace();
      return false;
    }

    Location location = new Location("");
    location.setLatitude(lat);
    location.setLongitude(lon);
    PushManager.sendLocation(cordova.getActivity(), location);

    return true;
  }
  private PluginResult internalSendLocation(JSONArray data, String callbackId) {
    if (mPushManager == null) {
      return new PluginResult(Status.ERROR);
    }

    JSONObject params = null;
    try {
      params = data.getJSONObject(0);
    } catch (JSONException e) {
      e.printStackTrace();
      return new PluginResult(Status.ERROR);
    }

    double lat = 0;
    double lon = 0;

    try {
      lat = params.getDouble("lat");
      lon = params.getDouble("lon");
    } catch (JSONException e) {
      e.printStackTrace();
      return new PluginResult(Status.ERROR);
    }

    Location location = new Location("");
    location.setLatitude(lat);
    location.setLongitude(lon);
    PushManager.sendLocation(cordova.getActivity(), location);

    return new PluginResult(Status.OK);
  }
  private boolean internalRegister(JSONArray data, CallbackContext callbackContext) {
    JSONObject params = null;
    try {
      params = data.getJSONObject(0);
    } catch (JSONException e) {
      e.printStackTrace();

      callbackContext.error(e.getMessage());
      return true;
    }

    callbackIds.put("registerDevice", callbackContext);

    try {
      String appid = null;
      if (params.has("appid")) appid = params.getString("appid");
      else appid = params.getString("pw_appid");

      mPushManager = new PushManager(cordova.getActivity(), appid, params.getString("projectid"));
    } catch (JSONException e) {
      callbackIds.remove("registerDevice");
      e.printStackTrace();

      callbackContext.error(e.getMessage());
      return true;
    }

    try {
      if (loggedStart) {
        mPushManager.onStartup(cordova.getActivity(), false);
      } else {
        mPushManager.onStartup(cordova.getActivity(), true);
        loggedStart = true;
      }
    } catch (java.lang.RuntimeException e) {
      callbackIds.remove("registerDevice");
      e.printStackTrace();

      callbackContext.error(e.getMessage());
      return true;
    }

    checkMessage(cordova.getActivity().getIntent());
    return true;
  }
示例#5
0
 @OnClick(R.id.debug_pushwoosh_copy_pushtoken)
 void onPushwooshCopyPushTokenButton() {
   final Context context = getContext().getApplicationContext();
   final String pushToken = PushManager.getPushToken(context);
   Timber.d("PushToken: %s", pushToken);
   ClipboardManager clipboard =
       (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
   ClipData clip = ClipData.newPlainText("label", pushToken);
   clipboard.setPrimaryClip(clip);
   Toast.makeText(context, "Copy PushToken to Buffer", Toast.LENGTH_SHORT).show();
 }
  private PluginResult internalRegister(JSONArray data, String callbackId) {
    JSONObject params = null;
    try {
      params = data.getJSONObject(0);
    } catch (JSONException e) {
      e.printStackTrace();
      return new PluginResult(Status.ERROR);
    }

    try {
      mPushManager =
          new PushManager(
              cordova.getActivity(), params.getString("appid"), params.getString("projectid"));
    } catch (JSONException e) {
      e.printStackTrace();
      return new PluginResult(Status.ERROR);
    }

    try {
      if (loggedStart) {
        mPushManager.onStartup(cordova.getActivity(), false);
      } else {
        mPushManager.onStartup(cordova.getActivity(), true);
        loggedStart = true;
      }
    } catch (java.lang.RuntimeException e) {
      e.printStackTrace();
      return new PluginResult(Status.ERROR);
    }

    checkMessage(cordova.getActivity().getIntent());

    callbackIds.put("registerDevice", callbackId);

    PluginResult result = new PluginResult(Status.NO_RESULT);
    result.setKeepCallback(true);
    return result;
  }
  private PluginResult internalSendTags(JSONArray data, String callbackId) {
    if (mPushManager == null) {
      return new PluginResult(Status.ERROR);
    }

    JSONObject params;
    try {
      params = data.getJSONObject(0);
    } catch (JSONException e) {
      e.printStackTrace();
      return new PluginResult(Status.ERROR);
    }

    @SuppressWarnings("unchecked")
    Iterator<String> nameItr = params.keys();
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    while (nameItr.hasNext()) {
      try {
        String name = nameItr.next();
        paramsMap.put(name, params.get(name));
      } catch (JSONException e) {
        e.printStackTrace();
        return new PluginResult(Status.ERROR);
      }
    }

    try {
      Map<String, String> skippedTags =
          PushManager.sendTagsFromBG(cordova.getActivity(), paramsMap);

      JSONObject skippedTagsObj = new JSONObject();
      for (String tagName : skippedTags.keySet()) {
        skippedTags.put(tagName, skippedTags.get(tagName));
      }

      return new PluginResult(Status.OK, skippedTagsObj);
    } catch (PushWooshException e) {
      e.printStackTrace();
      return new PluginResult(Status.ERROR);
    }
  }
  private boolean internalSendTags(JSONArray data, CallbackContext callbackContext) {
    JSONObject params;
    try {
      params = data.getJSONObject(0);
    } catch (JSONException e) {
      e.printStackTrace();
      return false;
    }

    @SuppressWarnings("unchecked")
    Iterator<String> nameItr = params.keys();
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    while (nameItr.hasNext()) {
      try {
        String name = nameItr.next();
        paramsMap.put(name, params.get(name));
      } catch (JSONException e) {
        e.printStackTrace();
        return false;
      }
    }

    try {
      Map<String, String> skippedTags =
          PushManager.sendTagsFromBG(cordova.getActivity(), paramsMap);

      JSONObject skippedTagsObj = new JSONObject();
      for (String tagName : skippedTags.keySet()) {
        skippedTags.put(tagName, skippedTags.get(tagName));
      }

      callbackContext.success(skippedTagsObj);
      return true;
    } catch (PushWooshException e) {
      e.printStackTrace();
      return false;
    }
  }
  @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;
  }