public void init(Context cnt, String tkn, String appid, String ServerUrl) throws CommunicatorConnectorException, IOException { mContext = cnt.getApplicationContext(); mUserAuthToken = tkn; mAppId = appid; mServerUrl = ServerUrl; CommunicatorConnector mConnector = null; try { mConnector = new CommunicatorConnector(mServerUrl, mAppId); String regId = ""; if (!checkPlayServices()) { return; } gcm = GoogleCloudMessaging.getInstance(mContext); // Get the existing registration id, if it exists. regId = readRegId(mContext); // GCMRegistrar.getRegistrationId(mContext); if (regId == null || regId.equals("")) { if (mConnector != null) { Map<String, Object> mapKey = mConnector.requestPublicConfigurationToPush(mAppId, mUserAuthToken); // find senderid String senderid = String.valueOf(mapKey.get("GCM_SENDER_ID")); // register this device for this project // senderid= "220741898329"; regId = gcm.register(senderid); if (regId != null && regId.length() > 0) { UserSignature signUserSignature = new UserSignature(); signUserSignature.setAppName(mAppId); signUserSignature.setRegistrationId(regId); mConnector.registerUserToPush(signUserSignature, mAppId, mUserAuthToken); writeRegId(mContext, regId); } } } else { // Already registered; } Log.i("GCM_REGID:", regId); } catch (Exception e) { gcm.unregister(); Log.e(TAG, e.toString()); } }
@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; }
public static void reset(Context mContext) throws IOException { writeRegId(mContext, ""); gcm.unregister(); }