Ejemplo n.º 1
0
  @Override
  public boolean execute(
      final String action, final JSONArray data, final CallbackContext callbackContext) {
    Log.v(LOG_TAG, "execute: action=" + action);
    gWebView = this.webView;

    if (INITIALIZE.equals(action)) {
      cordova
          .getThreadPool()
          .execute(
              new Runnable() {
                public void run() {
                  pushContext = callbackContext;
                  JSONObject jo = null;

                  Log.v(LOG_TAG, "execute: data=" + data.toString());
                  SharedPreferences sharedPref =
                      getApplicationContext()
                          .getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
                  String token = null;
                  String senderID = null;

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

                    Log.v(LOG_TAG, "execute: jo=" + jo.toString());

                    senderID = jo.getString(SENDER_ID);

                    Log.v(LOG_TAG, "execute: senderID=" + senderID);

                    String savedSenderID = sharedPref.getString(SENDER_ID, "");
                    String savedRegID = sharedPref.getString(REGISTRATION_ID, "");

                    // first time run get new token
                    if ("".equals(savedRegID)) {
                      token =
                          InstanceID.getInstance(getApplicationContext()).getToken(senderID, GCM);
                    }
                    // new sender ID, re-register
                    else if (!savedSenderID.equals(senderID)) {
                      token =
                          InstanceID.getInstance(getApplicationContext()).getToken(senderID, GCM);
                    }
                    // use the saved one
                    else {
                      token = sharedPref.getString(REGISTRATION_ID, "");
                    }

                    if (!"".equals(token)) {
                      JSONObject json = new JSONObject().put(REGISTRATION_ID, token);

                      Log.v(LOG_TAG, "onRegistered: " + json.toString());

                      JSONArray topics = jo.optJSONArray(TOPICS);
                      subscribeToTopics(topics, token);

                      PushPlugin.sendEvent(json);
                    } else {
                      callbackContext.error("Empty registration ID received from GCM");
                      return;
                    }
                  } catch (JSONException e) {
                    Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage());
                    callbackContext.error(e.getMessage());
                  } catch (IOException e) {
                    Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage());
                    callbackContext.error(e.getMessage());
                  }

                  if (jo != null) {
                    SharedPreferences.Editor editor = sharedPref.edit();
                    try {
                      editor.putString(ICON, jo.getString(ICON));
                    } catch (JSONException e) {
                      Log.d(LOG_TAG, "no icon option");
                    }
                    try {
                      editor.putString(ICON_COLOR, jo.getString(ICON_COLOR));
                    } catch (JSONException e) {
                      Log.d(LOG_TAG, "no iconColor option");
                    }

                    boolean clearBadge = jo.optBoolean(CLEAR_BADGE, false);
                    if (clearBadge) {
                      setApplicationIconBadgeNumber(getApplicationContext(), 0);
                    }

                    editor.putBoolean(SOUND, jo.optBoolean(SOUND, true));
                    editor.putBoolean(VIBRATE, jo.optBoolean(VIBRATE, true));
                    editor.putBoolean(CLEAR_BADGE, clearBadge);
                    editor.putBoolean(
                        CLEAR_NOTIFICATIONS, jo.optBoolean(CLEAR_NOTIFICATIONS, true));
                    editor.putBoolean(FORCE_SHOW, jo.optBoolean(FORCE_SHOW, false));
                    editor.putString(SENDER_ID, senderID);
                    editor.putString(REGISTRATION_ID, token);
                    editor.commit();
                  }

                  if (gCachedExtras != null) {
                    Log.v(LOG_TAG, "sending cached extras");
                    sendExtras(gCachedExtras);
                    gCachedExtras = null;
                  }
                }
              });
    } else if (UNREGISTER.equals(action)) {
      cordova
          .getThreadPool()
          .execute(
              new Runnable() {
                public void run() {
                  try {
                    SharedPreferences sharedPref =
                        getApplicationContext()
                            .getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
                    String token = sharedPref.getString(REGISTRATION_ID, "");
                    JSONArray topics = data.optJSONArray(0);
                    if (topics != null && !"".equals(token)) {
                      unsubscribeFromTopics(topics, token);
                    } else {
                      InstanceID.getInstance(getApplicationContext()).deleteInstanceID();
                      Log.v(LOG_TAG, "UNREGISTER");

                      // Remove shared prefs
                      SharedPreferences.Editor editor = sharedPref.edit();
                      editor.remove(SOUND);
                      editor.remove(VIBRATE);
                      editor.remove(CLEAR_BADGE);
                      editor.remove(CLEAR_NOTIFICATIONS);
                      editor.remove(FORCE_SHOW);
                      editor.remove(SENDER_ID);
                      editor.remove(REGISTRATION_ID);
                      editor.commit();
                    }

                    callbackContext.success();
                  } catch (IOException e) {
                    Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage());
                    callbackContext.error(e.getMessage());
                  }
                }
              });
    } else if (FINISH.equals(action)) {
      callbackContext.success();
    } else if (HAS_PERMISSION.equals(action)) {
      cordova
          .getThreadPool()
          .execute(
              new Runnable() {
                public void run() {
                  JSONObject jo = new JSONObject();
                  try {
                    jo.put(
                        "isEnabled",
                        PermissionUtils.hasPermission(
                            getApplicationContext(), "OP_POST_NOTIFICATION"));
                    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jo);
                    pluginResult.setKeepCallback(true);
                    callbackContext.sendPluginResult(pluginResult);
                  } catch (UnknownError e) {
                    callbackContext.error(e.getMessage());
                  } catch (JSONException e) {
                    callbackContext.error(e.getMessage());
                  }
                }
              });
    } else if (SET_APPLICATION_ICON_BADGE_NUMBER.equals(action)) {
      cordova
          .getThreadPool()
          .execute(
              new Runnable() {
                public void run() {
                  Log.v(LOG_TAG, "setApplicationIconBadgeNumber: data=" + data.toString());
                  try {
                    setApplicationIconBadgeNumber(
                        getApplicationContext(), data.getJSONObject(0).getInt(BADGE));
                  } catch (JSONException e) {
                    callbackContext.error(e.getMessage());
                  }
                  callbackContext.success();
                }
              });
    } else if (CLEAR_ALL_NOTIFICATIONS.equals(action)) {
      cordova
          .getThreadPool()
          .execute(
              new Runnable() {
                public void run() {
                  Log.v(LOG_TAG, "clearAllNotifications");
                  clearAllNotifications();
                  callbackContext.success();
                }
              });
    } else {
      Log.e(LOG_TAG, "Invalid action : " + action);
      callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
      return false;
    }

    return true;
  }
Ejemplo n.º 2
0
  @Override
  public boolean execute(
      final String action, final JSONArray data, final CallbackContext callbackContext) {
    Log.v(LOG_TAG, "execute: action=" + action);
    gWebView = this.webView;

    if (HAS_COLD_START_NOTIFICATION.equals(action)) {
      Log.v(LOG_TAG, "checking coldstart notification presence (" + gCachedExtras + ")");
      callbackContext.success((gCachedExtras != null) ? "true" : "false");
    } else if (INITIALIZE.equals(action)) {
      cordova
          .getThreadPool()
          .execute(
              new Runnable() {
                public void run() {
                  pushContext = callbackContext;
                  JSONObject jo = null;

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

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

                    Log.v(LOG_TAG, "execute: jo=" + jo.toString());

                    String senderID = jo.getString(SENDER_ID);

                    Log.v(LOG_TAG, "execute: senderID=" + senderID);

                    GCMRegistrar.register(getApplicationContext(), senderID);
                  } catch (JSONException e) {
                    Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage());
                    callbackContext.error(e.getMessage());
                  }

                  if (jo != null) {
                    SharedPreferences sharedPref =
                        getApplicationContext()
                            .getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPref.edit();
                    try {
                      editor.putString(ICON, jo.getString(ICON));
                    } catch (JSONException e) {
                      Log.d(LOG_TAG, "no icon option");
                    }
                    try {
                      editor.putString(ICON_COLOR, jo.getString(ICON_COLOR));
                    } catch (JSONException e) {
                      Log.d(LOG_TAG, "no iconColor option");
                    }
                    editor.putBoolean(SOUND, jo.optBoolean(SOUND, true));
                    editor.putBoolean(VIBRATE, jo.optBoolean(VIBRATE, true));
                    editor.putBoolean(
                        CLEAR_NOTIFICATIONS, jo.optBoolean(CLEAR_NOTIFICATIONS, true));
                    editor.putBoolean(FORCE_SHOW, jo.optBoolean(FORCE_SHOW, false));
                    editor.commit();
                  }

                  if (gCachedExtras != null) {
                    Log.v(LOG_TAG, "sending cached extras");
                    sendExtras(gCachedExtras);
                    gCachedExtras = null;
                  }
                }
              });
    } else if (UNREGISTER.equals(action)) {
      cordova
          .getThreadPool()
          .execute(
              new Runnable() {
                public void run() {
                  GCMRegistrar.unregister(getApplicationContext());

                  Log.v(LOG_TAG, "UNREGISTER");
                  callbackContext.success();
                }
              });
    } else {
      Log.e(LOG_TAG, "Invalid action : " + action);
      callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
      return false;
    }

    return true;
  }