diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 0c226618..508d88b3 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -58,6 +58,7 @@ body: label: Your Pushwoosh Cordova plugin version description: Your version of the Cordova plugin integrated into the application. You may find it on the [releases page](https://github.com/Pushwoosh/pushwoosh-phonegap-plugin/releases) options: + - 8.3.35 - 8.3.34 - 8.3.33 - 8.3.28 diff --git a/README.md b/README.md index 890116a0..246b5433 100644 --- a/README.md +++ b/README.md @@ -14,13 +14,13 @@ Cross-Platform push notifications by Pushwoosh for Cordova / PhoneGap Using npm: ``` -cordova plugin add pushwoosh-cordova-plugin@8.3.34 +cordova plugin add pushwoosh-cordova-plugin@8.3.35 ``` Using git: ``` -cordova plugin add https://github.com/Pushwoosh/pushwoosh-phonegap-plugin.git#8.3.34 +cordova plugin add https://github.com/Pushwoosh/pushwoosh-phonegap-plugin.git#8.3.35 ``` ### Guide diff --git a/package.json b/package.json index eb39689d..f359f6b0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pushwoosh-cordova-plugin", - "version": "8.3.34", + "version": "8.3.35", "description": "\n This plugin allows you to send and receive push notifications. Powered by Pushwoosh (www.pushwoosh.com).\n ", "main":"www/PushNotification.js", "typings":"types/index.d.ts", diff --git a/plugin.xml b/plugin.xml index 285cba09..3a8094f1 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,5 +1,5 @@ - + Pushwoosh @@ -77,13 +77,13 @@ - - - - - - - + + + + + + + @@ -125,7 +125,7 @@ - + diff --git a/src/android/android.iml b/src/android/android.iml new file mode 100644 index 00000000..c90834f2 --- /dev/null +++ b/src/android/android.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/android/src/com/pushwoosh/plugin/pushnotifications/InboxUiStyleManager.java b/src/android/src/com/pushwoosh/plugin/pushnotifications/InboxUiStyleManager.java index 8952bbaa..5e9d07eb 100644 --- a/src/android/src/com/pushwoosh/plugin/pushnotifications/InboxUiStyleManager.java +++ b/src/android/src/com/pushwoosh/plugin/pushnotifications/InboxUiStyleManager.java @@ -50,8 +50,9 @@ private static Integer parseColor(String colorString) { private Integer optColor(String key, Integer defaultValue) { Integer color = parseColor(style.optString(key)); - if (color == null) + if (color == null) { return defaultValue; + } return color; } @@ -84,8 +85,7 @@ private Drawable optImage(String key, Drawable defaultValue) { if (imagePath != null && imagePath.length() != 0) { try { return getDrawable(imagePath); - } - catch (IOException e) { + } catch (IOException e) { e.printStackTrace(); } @@ -129,8 +129,9 @@ public void parse() { } public static void setStyle(Context context, JSONObject styleObject) { - if (styleObject != null) + if (styleObject != null) { new StyleParser(context, styleObject).parse(); + } } } diff --git a/src/android/src/com/pushwoosh/plugin/pushnotifications/PushNotifications.java b/src/android/src/com/pushwoosh/plugin/pushnotifications/PushNotifications.java index b74eafb2..448dd1e1 100644 --- a/src/android/src/com/pushwoosh/plugin/pushnotifications/PushNotifications.java +++ b/src/android/src/com/pushwoosh/plugin/pushnotifications/PushNotifications.java @@ -111,8 +111,9 @@ public void onDestroy() { } private JSONObject getPushFromIntent(Intent intent) { - if (null == intent) + if (null == intent) { return null; + } if (intent.hasExtra(Pushwoosh.PUSH_RECEIVE_EVENT)) { String pushString = intent.getExtras().getString(Pushwoosh.PUSH_RECEIVE_EVENT); @@ -224,15 +225,11 @@ public void process(@NonNull final Result result) { return; } - if(result.isSuccess()){ + if(result.isSuccess()) { callback.success(new JSONObject()); - } else if(result.getException()!=null){ + } else if(result.getException()!=null) { callback.error(result.getException().getMessage()); } @@ -292,8 +286,9 @@ private boolean getTags(JSONArray data, final CallbackContext callbackContext) { @Override public void process(@NonNull final Result result) { CallbackContext callback = callbackIds.get("getTags"); - if (callback == null) + if (callback == null) { return; + } if(result.isSuccess()) { callback.success(result.getData().toJson()); @@ -319,21 +314,16 @@ private boolean getPushwooshHWID(JSONArray data, final CallbackContext callbackC } @CordovaMethod - private boolean createLocalNotification(JSONArray data, final CallbackContext callbackContext) - { + private boolean createLocalNotification(JSONArray data, final CallbackContext callbackContext) { JSONObject params = null; - try - { + try { params = data.getJSONObject(0); - } - catch (JSONException e) - { + } catch (JSONException e) { PWLog.error(TAG, "No parameters passed (missing parameters)", e); return false; } - try - { + try { //config params: {msg:"message", seconds:30, userData:"optional"} String message = params.getString("msg"); int seconds = params.getInt("seconds"); @@ -352,9 +342,7 @@ private boolean createLocalNotification(JSONArray data, final CallbackContext ca .setExtras(extras) .build(); Pushwoosh.getInstance().scheduleLocalNotification(notification); - } - catch (JSONException e) - { + } catch (JSONException e) { PWLog.error(TAG, "Not correct parameters passed (missing parameters)", e); return false; } @@ -363,15 +351,13 @@ private boolean createLocalNotification(JSONArray data, final CallbackContext ca } @CordovaMethod - private boolean clearLocalNotification(JSONArray data, final CallbackContext callbackContext) - { + private boolean clearLocalNotification(JSONArray data, final CallbackContext callbackContext) { LocalNotificationReceiver.cancelAll(); return true; } @CordovaMethod - private boolean getLaunchNotification(JSONArray data, final CallbackContext callbackContext) - { + private boolean getLaunchNotification(JSONArray data, final CallbackContext callbackContext) { PushMessage launchNotification = Pushwoosh.getInstance().getLaunchNotification(); if (launchNotification == null) { callbackContext.success((String) null); @@ -382,39 +368,33 @@ private boolean getLaunchNotification(JSONArray data, final CallbackContext call } @CordovaMethod - private boolean clearLaunchNotification(JSONArray data, final CallbackContext callbackContext) - { + private boolean clearLaunchNotification(JSONArray data, final CallbackContext callbackContext) { Pushwoosh.getInstance().clearLaunchNotification(); return true; } @CordovaMethod - private boolean setMultiNotificationMode(JSONArray data, final CallbackContext callbackContext) - { + private boolean setMultiNotificationMode(JSONArray data, final CallbackContext callbackContext) { PushwooshNotificationSettings.setMultiNotificationMode(true); return true; } @CordovaMethod - private boolean setSingleNotificationMode(JSONArray data, final CallbackContext callbackContext) - { + private boolean setSingleNotificationMode(JSONArray data, final CallbackContext callbackContext) { PushwooshNotificationSettings.setMultiNotificationMode(false); return true; } @CordovaMethod - private boolean setSoundType(JSONArray data, final CallbackContext callbackContext) - { - try - { + private boolean setSoundType(JSONArray data, final CallbackContext callbackContext) { + try { Integer type = (Integer) data.get(0); - if (type == null) + if (type == null) { return false; + } PushwooshNotificationSettings.setSoundNotificationType(SoundType.fromInt(type)); - } - catch (Exception e) - { + } catch (Exception e) { PWLog.error(TAG, "No sound parameters passed (missing parameters)", e); return false; } @@ -423,18 +403,15 @@ private boolean setSoundType(JSONArray data, final CallbackContext callbackConte } @CordovaMethod - private boolean setVibrateType(JSONArray data, final CallbackContext callbackContext) - { - try - { + private boolean setVibrateType(JSONArray data, final CallbackContext callbackContext) { + try { Integer type = (Integer) data.get(0); - if (type == null) + if (type == null) { return false; + } PushwooshNotificationSettings.setVibrateNotificationType(VibrateType.fromInt(type)); - } - catch (Exception e) - { + } catch (Exception e) { PWLog.error(TAG, "No vibration parameters passed (missing parameters)", e); return false; } @@ -443,15 +420,11 @@ private boolean setVibrateType(JSONArray data, final CallbackContext callbackCon } @CordovaMethod - private boolean setLightScreenOnNotification(JSONArray data, final CallbackContext callbackContext) - { - try - { + private boolean setLightScreenOnNotification(JSONArray data, final CallbackContext callbackContext) { + try { boolean type = (boolean) data.getBoolean(0); PushwooshNotificationSettings.setLightScreenOnNotification(type); - } - catch (Exception e) - { + } catch (Exception e) { PWLog.error(TAG, "No parameters passed (missing parameters)", e); return false; } @@ -460,15 +433,11 @@ private boolean setLightScreenOnNotification(JSONArray data, final CallbackConte } @CordovaMethod - private boolean setEnableLED(JSONArray data, final CallbackContext callbackContext) - { - try - { + private boolean setEnableLED(JSONArray data, final CallbackContext callbackContext) { + try { boolean type = (boolean) data.getBoolean(0); PushwooshNotificationSettings.setEnableLED(type); - } - catch (Exception e) - { + } catch (Exception e) { PWLog.error(TAG, "No parameters passed (missing parameters)", e); return false; } @@ -477,19 +446,16 @@ private boolean setEnableLED(JSONArray data, final CallbackContext callbackConte } @CordovaMethod - private boolean setColorLED(JSONArray data, final CallbackContext callbackContext) - { - try - { + private boolean setColorLED(JSONArray data, final CallbackContext callbackContext) { + try { String colorString = (String) data.get(0); - if (colorString == null) + if (colorString == null) { return false; + } int colorLed = GeneralUtils.parseColor(colorString); PushwooshNotificationSettings.setColorLED(colorLed); - } - catch (Exception e) - { + } catch (Exception e) { PWLog.error(TAG, "No parameters passed (missing parameters)", e); return false; } @@ -498,12 +464,11 @@ private boolean setColorLED(JSONArray data, final CallbackContext callbackContex } @CordovaMethod - private boolean getPushHistory(JSONArray data, final CallbackContext callbackContext) - { + private boolean getPushHistory(JSONArray data, final CallbackContext callbackContext) { List pushMessageHistory = Pushwoosh.getInstance().getPushHistory(); List pushHistory = new ArrayList(); - for (PushMessage pushMessage: pushMessageHistory){ + for (PushMessage pushMessage: pushMessageHistory) { pushHistory.add(pushMessage.toJson().toString()); } callbackContext.success(new JSONArray(pushHistory)); @@ -511,29 +476,23 @@ private boolean getPushHistory(JSONArray data, final CallbackContext callbackCon } @CordovaMethod - private boolean clearPushHistory(JSONArray data, final CallbackContext callbackContext) - { + private boolean clearPushHistory(JSONArray data, final CallbackContext callbackContext) { Pushwoosh.getInstance().clearPushHistory(); return true; } @CordovaMethod - private boolean clearNotificationCenter(JSONArray data, final CallbackContext callbackContext) - { + private boolean clearNotificationCenter(JSONArray data, final CallbackContext callbackContext) { NotificationManagerCompat.from(cordova.getActivity()).cancelAll(); return true; } @CordovaMethod - private boolean setApplicationIconBadgeNumber(JSONArray data, final CallbackContext callbackContext) - { - try - { + private boolean setApplicationIconBadgeNumber(JSONArray data, final CallbackContext callbackContext) { + try { Integer badgeNumber = data.getJSONObject(0).getInt("badge"); PushwooshBadge.setBadgeNumber(badgeNumber); - } - catch (JSONException e) - { + } catch (JSONException e) { PWLog.error(TAG, "No parameters passed (missing parameters)", e); return false; } @@ -541,23 +500,18 @@ private boolean setApplicationIconBadgeNumber(JSONArray data, final CallbackCont } @CordovaMethod - private boolean getApplicationIconBadgeNumber(JSONArray data, final CallbackContext callbackContext) - { + private boolean getApplicationIconBadgeNumber(JSONArray data, final CallbackContext callbackContext) { Integer badgeNumber = PushwooshBadge.getBadgeNumber(); callbackContext.success(badgeNumber); return true; } @CordovaMethod - private boolean addToApplicationIconBadgeNumber(JSONArray data, final CallbackContext callbackContext) - { - try - { + private boolean addToApplicationIconBadgeNumber(JSONArray data, final CallbackContext callbackContext) { + try { Integer badgeNumber = data.getJSONObject(0).getInt("badge"); PushwooshBadge.addBadgeNumber(badgeNumber); - } - catch (JSONException e) - { + } catch (JSONException e) { PWLog.error(TAG, "No parameters passed (missing parameters)", e); return false; } @@ -565,58 +519,46 @@ private boolean addToApplicationIconBadgeNumber(JSONArray data, final CallbackCo } @CordovaMethod - private boolean setUserId(JSONArray data, final CallbackContext callbackContext) - { - try - { + private boolean setUserId(JSONArray data, final CallbackContext callbackContext) { + try { String userId = data.getString(0); PushwooshInApp.getInstance().setUserId(userId); - } - catch (JSONException e) - { + } catch (JSONException e) { PWLog.error(TAG, "No parameters passed (missing parameters)", e); } return true; } @CordovaMethod - private boolean postEvent(JSONArray data, final CallbackContext callbackContext) - { - try - { + private boolean postEvent(JSONArray data, final CallbackContext callbackContext) { + try { String event = data.getString(0); JSONObject attributes = data.getJSONObject(1); PushwooshInApp.getInstance().postEvent(event, Tags.fromJson(attributes)); - } - catch (JSONException e) - { + } catch (JSONException e) { PWLog.error(TAG, "No parameters passed (missing parameters)", e); } return true; } @CordovaMethod - private boolean getRemoteNotificationStatus(JSONArray data, final CallbackContext callbackContext) - { - try - { + private boolean getRemoteNotificationStatus(JSONArray data, final CallbackContext callbackContext) { + try { String enabled = PushwooshNotificationSettings.areNotificationsEnabled() ? "1" : "0"; JSONObject result = new JSONObject(); result.put("enabled", enabled); callbackContext.success(result); - } - catch (Exception e) - { + } catch (Exception e) { callbackContext.error(e.getMessage()); } - return true; } @CordovaMethod private boolean presentInboxUI(JSONArray data, final CallbackContext callbackContext) { - if (data.length() > 0) + if (data.length() > 0) { InboxUiStyleManager.setStyle(this.cordova.getActivity(), data.optJSONObject(0)); + } this.cordova.getActivity().startActivity(new Intent(this.cordova.getActivity(), InboxActivity.class)); return true; } @@ -629,17 +571,18 @@ private boolean loadMessages(JSONArray data, final CallbackContext callbackConte @Override public void process(@NonNull Result, InboxMessagesException> result) { CallbackContext callback = callbackIds.get("loadMessages"); - if (callback == null) + if (callback == null) { return; + } if(result.isSuccess() && result.getData() != null) { ArrayList messagesList = new ArrayList<>(result.getData()); JSONArray jsonArray = new JSONArray(); - for (InboxMessage message : messagesList){ + for (InboxMessage message : messagesList) { jsonArray.put(inboxMessageToJson(message)); } callback.success(jsonArray); - } else if (result.getException() != null){ + } else if (result.getException() != null) { callback.error(result.getException().getMessage()); } } @@ -662,12 +605,13 @@ public boolean messagesWithNoActionPerformedCount(JSONArray data, final Callback @Override public void process(@NonNull Result result) { CallbackContext callback = callbackIds.get("messagesWithNoActionPerformedCount"); - if (callback == null) + if (callback == null) { return; + } if(result.isSuccess() && result.getData() != null) { callback.success(result.getData()); - } else if (result.getException() != null){ + } else if (result.getException() != null) { callback.error(result.getException().getMessage()); } } @@ -690,12 +634,13 @@ public boolean unreadMessagesCount(JSONArray data, final CallbackContext callbac @Override public void process(@NonNull Result result) { CallbackContext callback = callbackIds.get("unreadMessagesCount"); - if (callback == null) + if (callback == null) { return; + } if(result.isSuccess() && result.getData() != null) { callback.success(result.getData()); - } else if (result.getException() != null){ + } else if (result.getException() != null) { callback.error(result.getException().getMessage()); } } @@ -718,12 +663,13 @@ public boolean messagesCount(JSONArray data, final CallbackContext callbackConte @Override public void process(@NonNull Result result) { CallbackContext callback = callbackIds.get("messagesCount"); - if (callback == null) + if (callback == null) { return; + } if(result.isSuccess() && result.getData() != null) { callback.success(result.getData()); - } else if (result.getException() != null){ + } else if (result.getException() != null) { callback.error(result.getException().getMessage()); } } @@ -772,26 +718,26 @@ public boolean performAction(JSONArray data, final CallbackContext callbackConte } @CordovaMethod - public boolean showGDPRConsentUI(JSONArray data, final CallbackContext callbackContext){ + public boolean showGDPRConsentUI(JSONArray data, final CallbackContext callbackContext) { GDPRManager.getInstance().showGDPRConsentUI(); return true; } @CordovaMethod - public boolean showGDPRDeletionUI(JSONArray data, final CallbackContext callbackContext){ + public boolean showGDPRDeletionUI(JSONArray data, final CallbackContext callbackContext) { GDPRManager.getInstance().showGDPRDeletionUI(); return true; } @CordovaMethod - public boolean isDeviceDataRemoved(JSONArray data, final CallbackContext callbackContext){ + public boolean isDeviceDataRemoved(JSONArray data, final CallbackContext callbackContext) { boolean removed = GDPRManager.getInstance().isDeviceDataRemoved(); callbackContext.success(removed ? 1 : 0); return true; } @CordovaMethod - public boolean isCommunicationEnabled(JSONArray data, final CallbackContext callbackContext){ + public boolean isCommunicationEnabled(JSONArray data, final CallbackContext callbackContext) { boolean enabled = GDPRManager.getInstance().isCommunicationEnabled(); callbackContext.success(enabled ? 1 : 0); return true; @@ -799,18 +745,18 @@ public boolean isCommunicationEnabled(JSONArray data, final CallbackContext call } @CordovaMethod - public boolean isAvailableGDPR(JSONArray data, final CallbackContext callbackContext){ + public boolean isAvailableGDPR(JSONArray data, final CallbackContext callbackContext) { boolean isAvailableGDPR = GDPRManager.getInstance().isAvailable(); callbackContext.success(isAvailableGDPR ? 1 : 0); return true; } @CordovaMethod - public boolean removeAllDeviceData(JSONArray data, final CallbackContext callbackContext){ + public boolean removeAllDeviceData(JSONArray data, final CallbackContext callbackContext) { GDPRManager.getInstance().removeAllDeviceData(new Callback() { @Override public void process(@NonNull Result result) { - if(result.isSuccess()){ + if(result.isSuccess()) { callbackContext.success(); }else { callbackContext.error(result.getException().getMessage()); @@ -821,13 +767,13 @@ public void process(@NonNull Result result) { } @CordovaMethod - public boolean setCommunicationEnabled(JSONArray data, final CallbackContext callbackContext){ + public boolean setCommunicationEnabled(JSONArray data, final CallbackContext callbackContext) { try { boolean enable = data.getBoolean(0); GDPRManager.getInstance().setCommunicationEnabled(enable, new Callback() { @Override public void process(@NonNull Result result) { - if(result.isSuccess()){ + if(result.isSuccess()) { callbackContext.success(); }else { callbackContext.error(result.getException().getMessage()); @@ -841,89 +787,168 @@ public void process(@NonNull Result result) { return false; } + @CordovaMethod + public boolean setEmail(JSONArray data, final CallbackContext callbackContext) { + try { + String email = data.getString(0); + Pushwoosh.getInstance().setEmail(email, result -> { + if (result.isSuccess()) { + callbackContext.success(); + } else { + callbackContext.error("Failed to set email"); + } + }); + } catch (Exception e) { + PWLog.error(TAG, "No parameters passed to setEmail (missing parameters)", e); + callbackContext.error(e.getMessage()); + } + return true; + } + + @CordovaMethod + public boolean setEmails(JSONArray data, final CallbackContext callbackContext) { + try { + JSONArray emailsArray = data.getJSONArray(0); + List emails = new ArrayList<>(emailsArray.length()); + for (int i = 0; i < emailsArray.length(); i++) { + emails.add(emailsArray.getString(i)); + } + Pushwoosh.getInstance().setEmail(emails, result -> { + if (result.isSuccess()) { + callbackContext.success(); + } else { + callbackContext.error("Failed to set emails"); + } + }); + } catch (Exception e) { + PWLog.error(TAG, "No parameters passed to setEmails (missing parameters)", e); + callbackContext.error(e.getMessage()); + } + return true; + } + + @CordovaMethod + public boolean setUserEmails(JSONArray data, final CallbackContext callbackContext) { + try { + String userId = data.getString(0); + JSONArray emailsArray = data.getJSONArray(1); + List emails = new ArrayList<>(emailsArray.length()); + for (int i = 0; i < emailsArray.length(); i++) { + emails.add(emailsArray.getString(i)); + } + + Pushwoosh.getInstance().setUser(userId, emails, result -> { + if (result.isSuccess()) { + callbackContext.success(); + } else { + callbackContext.error("Failed to set user emails"); + } + }); + } catch (Exception e) { + PWLog.error(TAG, "No parameters passed to setUserEmails (missing parameters)", e); + callbackContext.error(e.getMessage()); + } + return true; + } + + @CordovaMethod + private boolean registerSMSNumber(JSONArray data, final CallbackContext callbackContext) { + try { + String phoneNumber = data.getString(0); + Pushwoosh.getInstance().registerSMSNumber(phoneNumber); + } catch (Exception e) { + PWLog.error(TAG, "No parameters passed (missing parameters)", e); + callbackContext.error(e.getMessage()); + } + return true; + } + + @CordovaMethod + private boolean registerWhatsappNumber(JSONArray data, final CallbackContext callbackContext) { + try { + String phoneNumber = data.getString(0); + Pushwoosh.getInstance().registerWhatsappNumber(phoneNumber); + } catch (Exception e) { + PWLog.error(TAG, "No parameters passed (missing parameters)", e); + callbackContext.error(e.getMessage()); + } + return true; + } + @CordovaMethod public boolean enableHuaweiPushNotifications(JSONArray data, final CallbackContext callbackContext) { Pushwoosh.getInstance().enableHuaweiPushNotifications(); return true; } - @Override - public boolean execute(String action, JSONArray data, CallbackContext callbackId) - { + public boolean execute(String action, JSONArray data, CallbackContext callbackId) { PWLog.debug(TAG, "Plugin Method Called: " + action); Method method = exportedMethods.get(action); - if (method == null) - { + if (method == null) { PWLog.debug(TAG, "Invalid action : " + action + " passed"); return false; } - try - { + try { Boolean result = (Boolean) method.invoke(this, data, callbackId); return result; } - catch (Exception e) - { + catch (Exception e) { PWLog.error(TAG, "Failed to execute action : " + action, e); return false; } } - private void doOnRegistered(String registrationId) - { + private void doOnRegistered(String registrationId) { CallbackContext callback = callbackIds.get("registerDevice"); - if (callback == null) + if (callback == null) { return; + } - try - { + try { JSONObject result = new JSONObject(); result.put("pushToken", registrationId); callback.success(result); - } - catch (Exception e) - { + } catch (Exception e) { callback.error("Internal error"); } callbackIds.remove("registerDevice"); } - private void doOnRegisteredError(String errorId) - { + private void doOnRegisteredError(String errorId) { CallbackContext callback = callbackIds.get("registerDevice"); - if (callback == null) + if (callback == null) { return; + } callback.error(errorId); callbackIds.remove("registerDevice"); } - private void doOnUnregistered(String registrationId) - { + private void doOnUnregistered(String registrationId) { CallbackContext callback = callbackIds.get("unregisterDevice"); - if (callback == null) + if (callback == null) { return; + } callback.success(registrationId); callbackIds.remove("unregisterDevice"); } - private void doOnUnregisteredError(String errorId) - { + private void doOnUnregisteredError(String errorId) { CallbackContext callback = callbackIds.get("unregisterDevice"); - if (callback == null) + if (callback == null) { return; + } callback.error(errorId); callbackIds.remove("unregisterDevice"); } - private void doOnPushOpened(String notification) - { + private void doOnPushOpened(String notification) { PWLog.debug(TAG, "push opened: " + notification); String jsStatement = String.format("cordova.require(\"pushwoosh-cordova-plugin.PushNotification\").notificationCallback(%s);", convertNotification(notification)); @@ -931,8 +956,7 @@ private void doOnPushOpened(String notification) sStartPushData = null; } - public void doOnPushReceived(String notification) - { + public void doOnPushReceived(String notification) { PWLog.debug(TAG, "push received: " + notification); String jsStatement = String.format("cordova.require(\"pushwoosh-cordova-plugin.PushNotification\").pushReceivedCallback(%s);", convertNotification(notification)); @@ -941,12 +965,10 @@ public void doOnPushReceived(String notification) sReceivedPushData = null; } - private String convertNotification(String notification) - { + private String convertNotification(String notification) { JSONObject unifiedNotification = new JSONObject(); - try - { + try { JSONObject notificationJson = new JSONObject(notification); String pushMessage = notificationJson.optString("title"); Boolean foreground = notificationJson.optBoolean("foreground"); @@ -959,8 +981,7 @@ private String convertNotification(String notification) unifiedNotification.put("foreground", foreground); unifiedNotification.put("onStart", onStart); unifiedNotification.put("userdata", userData); - } - catch (JSONException e) { + } catch (JSONException e) { PWLog.error(TAG, "push message parsing failed", e); } @@ -972,21 +993,15 @@ private String convertNotification(String notification) return result; } - private void evalJs(String statement) - { + private void evalJs(String statement) { final String url = "javascript:" + statement; - handler.post(new Runnable() - { + handler.post(new Runnable() { @Override - public void run() - { - try - { + public void run() { + try { webView.loadUrl(url); - } - catch (Exception e) - { + } catch (Exception e) { PWLog.exception(e); } } diff --git a/src/ios/PushNotification.m b/src/ios/PushNotification.m index a828574a..a2680c4b 100755 --- a/src/ios/PushNotification.m +++ b/src/ios/PushNotification.m @@ -674,6 +674,64 @@ - (void)setCommunicationEnabled:(CDVInvokedUrlCommand *)command { }]; } +- (void)setEmail:(CDVInvokedUrlCommand *)command { + NSString *email = command.arguments[0]; + [[Pushwoosh sharedInstance] setEmail:email completion:^(NSError *error) { + CDVPluginResult *pluginResult; + if (!error) { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + } else { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Failed to set email"]; + } + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; +} + +- (void)setEmails:(CDVInvokedUrlCommand *)command { + NSArray *emailsArray = command.arguments[0]; + for (NSString *email in emailsArray) { + [[Pushwoosh sharedInstance] setEmail:email completion:^(NSError *error) { + CDVPluginResult *pluginResult; + if (!error) { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + } else { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Failed to set emails"]; + } + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + } +} + +- (void)setUserEmails:(CDVInvokedUrlCommand *)command { + NSString *userId = command.arguments[0]; + NSArray *emailsArray = command.arguments[1]; + for (NSString *email in emailsArray) { + [[Pushwoosh sharedInstance] setUser:userId emails:@[email] completion:^(NSError *error) { + CDVPluginResult *pluginResult; + if (!error) { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + } else { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Failed to set user emails"]; + } + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + } +} + +- (void)registerSMSNumber:(CDVInvokedUrlCommand *)command { + NSString *phoneNumber = command.arguments[0]; + [[Pushwoosh sharedInstance] registerSmsNumber:phoneNumber]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + +- (void)registerWhatsappNumber:(CDVInvokedUrlCommand *)command { + NSString *phoneNumber = command.arguments[0]; + [[Pushwoosh sharedInstance] registerWhatsappNumber:phoneNumber]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + - (void)removeAllDeviceData:(CDVInvokedUrlCommand *)command { self.callbackIds[@"removeAllDeviceData"] = command.callbackId; diff --git a/types/PushNotification.d.ts b/types/PushNotification.d.ts index b3510fdf..c1799a66 100644 --- a/types/PushNotification.d.ts +++ b/types/PushNotification.d.ts @@ -87,4 +87,9 @@ export interface PushNotification { setColorLED(color: string, success?: () => void, fail?: () => void): void; getPushHistory(success?: (pushHistory: Object) => void): void; clearPushHistory(callback: () => void): void; + setEmail(email: string, success?: () => void, fail?: (error: Error|string) => void): void; + setEmails(emails: string[], success?: () => void, fail?: (error: Error|string) => void): void; + setUserEmails(userId: string, emails: string[], success?: () => void, fail?: (error: Error|string) => void): void; + registerSMSNumber(phoneNumber: string, success?: () => void, fail?: (error: Error|string) => void): void; + registerWhatsappNumber(phoneNumber: string, success?: () => void, fail?: (error: Error|string) => void): void; } \ No newline at end of file diff --git a/www/PushNotification.js b/www/PushNotification.js index 67aff932..1aaa4d89 100755 --- a/www/PushNotification.js +++ b/www/PushNotification.js @@ -614,6 +614,36 @@ PushNotification.prototype.setCommunicationEnabled = function(enable, success, f exec(success, fail, "PushNotification", "setCommunicationEnabled", [enable]); }; +// Register email associated to the current user. +// Email should be a string and could not be null or empty. +PushNotification.prototype.setEmail = function(email, success, fail) { + exec(success, fail, "PushNotification", "setEmail", [email]); +}; + +// Register list of emails associated to the current user. +PushNotification.prototype.setEmails = function(emails, success, fail) { + exec(success, fail, "PushNotification", "setEmails", [emails]); +}; + +// Set user identifier and register emails associated to the user. +// userID can be Facebook ID or any other user ID. +// This allows data and events to be matched across multiple user devices. +PushNotification.prototype.setUserEmails = function(userId, emails, success, fail) { + exec(success, fail, "PushNotification", "setUserEmails", [userId, emails]); +}; + +// Registers phone number associated to the current user. +// PhoneNumber should be a string and cannot be null or empty. +PushNotification.prototype.registerSMSNumber = function(phoneNumber, success, fail) { + exec(success, fail, "PushNotification", "registerSMSNumber", [phoneNumber]); +}; + +// Registers Whatsapp number associated to the current user. +// PhoneNumber should be a string and cannot be null or empty. +PushNotification.prototype.registerWhatsappNumber = function(phoneNumber, success, fail) { + exec(success, fail, "PushNotification", "registerWhatsappNumber", [phoneNumber]); +}; + // Removes all device data from Pushwoosh and stops all interactions and communication permanently. PushNotification.prototype.removeAllDeviceData = function(success, fail) { exec(success, fail, "PushNotification", "removeAllDeviceData", []);