cordova-plugin-appice 2.0.10 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,12 @@
1
1
  package com.appice.cordova;
2
2
 
3
- import android.content.ComponentName;
4
3
  import android.content.Context;
5
4
  import android.content.Intent;
5
+ import android.content.SharedPreferences;
6
6
  import android.content.pm.PackageManager;
7
7
  import android.content.pm.ResolveInfo;
8
8
  import android.os.Build;
9
+ import android.os.Bundle;
9
10
  import android.os.Handler;
10
11
  import android.os.Looper;
11
12
  import android.util.DisplayMetrics;
@@ -34,19 +35,25 @@ import semusi.activitysdk.ContextData;
34
35
  import semusi.activitysdk.ContextSdk;
35
36
  import semusi.activitysdk.SdkConfig;
36
37
  import semusi.activitysdk.User;
38
+ import semusi.context.utility.Utility;
39
+
37
40
  import static java.lang.annotation.RetentionPolicy.RUNTIME;
38
41
 
39
42
  public class AppICEPlugin extends CordovaPlugin {
40
43
 
44
+ private final String CALLBACK_CONSTANTS = "pendingCallback";
45
+ private static final String TEMP_DB = "AppICETempDB";
46
+
47
+ private static boolean statusFlag = false;
41
48
  private static final String TAG = AppICEPlugin.class.getSimpleName();
42
49
  private static final Map<String, Method> exportedMethods;
43
50
  private static boolean inBackground = true;
44
- private static ArrayList<JSONObject> notificationStack = null;
45
51
  private static CallbackContext notificationCallbackContext;
46
52
  private static CallbackContext callbackContext;
47
53
 
48
54
  @Retention(RUNTIME)
49
- @interface CordovaMethod { }
55
+ @interface CordovaMethod {
56
+ }
50
57
 
51
58
  static {
52
59
  HashMap<String, Method> methods = new HashMap<String, Method>();
@@ -67,7 +74,6 @@ public class AppICEPlugin extends CordovaPlugin {
67
74
 
68
75
  @Override
69
76
  public void onResume(boolean multitasking) {
70
- AppICEPlugin.inBackground = false;
71
77
  }
72
78
 
73
79
  @Override
@@ -75,6 +81,15 @@ public class AppICEPlugin extends CordovaPlugin {
75
81
  AppICEPlugin.notificationCallbackContext = null;
76
82
  }
77
83
 
84
+ @Override
85
+ public void onDestroy() {
86
+ }
87
+
88
+ @Override
89
+ public void onStop() {
90
+ statusFlag = false;
91
+ }
92
+
78
93
  public static boolean inBackground() {
79
94
  return AppICEPlugin.inBackground;
80
95
  }
@@ -87,9 +102,39 @@ public class AppICEPlugin extends CordovaPlugin {
87
102
  public void initialize(CordovaInterface cordova, CordovaWebView webView) {
88
103
  super.initialize(cordova, webView);
89
104
 
105
+
90
106
  onNewIntent(cordova.getActivity().getIntent());
91
107
  }
92
108
 
109
+ public void onNewIntent(Intent intent) {
110
+ if (intent == null) return;
111
+ try {
112
+ Bundle extras = intent.getExtras();
113
+ String payload = extras.getString("ai_content");
114
+ boolean isPushNotification = (payload != null && payload.length() > 0) ? true : false;
115
+ if (isPushNotification) {
116
+ JSONObject data = new JSONObject(payload);
117
+
118
+ final String json = "{'payload':" + data.toString() + "}";
119
+ System.out.println("AppICE : onNewIntent : json "+json);
120
+ if (statusFlag) {
121
+ webView.getView().post(new Runnable() {
122
+ @Override
123
+ public void run() {
124
+ webView.loadUrl("javascript:cordova.fireDocumentEvent('pushNotificationClicked'," + json + ");");
125
+ }
126
+ });
127
+ } else {
128
+ storeTempData(CALLBACK_CONSTANTS, json, cordova.getActivity());
129
+ }
130
+
131
+ ContextSdk.pushNotificationClicked(payload, cordova.getActivity().getApplicationContext());
132
+ }
133
+ } catch (Throwable throwable) {
134
+ Log.e(TAG, "onNewIntent: error ", throwable);
135
+ }
136
+ }
137
+
93
138
  @Override
94
139
  public boolean execute(String action, JSONArray data, CallbackContext callbackId) {
95
140
  Method method = exportedMethods.get(action);
@@ -197,6 +242,31 @@ public class AppICEPlugin extends CordovaPlugin {
197
242
  }
198
243
  }
199
244
 
245
+ @CordovaMethod
246
+ private void isDeviceReady(JSONArray data, CallbackContext callbackContext) {
247
+ try {
248
+ if (statusFlag==false) {
249
+ checkIfAnyPendingCallback(cordova.getActivity());
250
+ }
251
+ statusFlag = true;
252
+ callbackContext.success();
253
+
254
+ } catch (Throwable e) {
255
+ callbackContext.error(e.getMessage());
256
+ }
257
+ }
258
+
259
+ private void checkIfAnyPendingCallback(Context context) {
260
+ String pendingCallbackStr = loadTempData(CALLBACK_CONSTANTS, context);
261
+ webView.getView().post(new Runnable() {
262
+ @Override
263
+ public void run() {
264
+ webView.loadUrl("javascript:cordova.fireDocumentEvent('pushNotificationClicked'," + pendingCallbackStr + ");");
265
+ resetTempData(CALLBACK_CONSTANTS, context);
266
+ }
267
+ });
268
+ }
269
+
200
270
  @CordovaMethod
201
271
  private void stopContext(JSONArray data, CallbackContext callbackContext) {
202
272
  try {
@@ -223,7 +293,7 @@ public class AppICEPlugin extends CordovaPlugin {
223
293
  };
224
294
  mainHandler.post(myRunnable);
225
295
 
226
- }catch (Throwable t){
296
+ } catch (Throwable t) {
227
297
  callbackContext.error(t.getMessage());
228
298
  }
229
299
  }
@@ -597,9 +667,9 @@ public class AppICEPlugin extends CordovaPlugin {
597
667
  }
598
668
 
599
669
  @CordovaMethod
600
- private void pushNotificationClicked(JSONArray data, final CallbackContext callbackContext){
670
+ private void pushNotificationClicked(JSONArray data, final CallbackContext callbackContext) {
601
671
  try {
602
- if(data != null){
672
+ if (data != null) {
603
673
  String payload = (String) data.getJSONObject(0).get("pushPayload");
604
674
  ContextSdk.pushNotificationClicked(payload, cordova.getActivity().getApplicationContext());
605
675
  callbackContext.success();
@@ -611,9 +681,9 @@ public class AppICEPlugin extends CordovaPlugin {
611
681
  }
612
682
 
613
683
  @CordovaMethod
614
- private void getDeepLinkURL(JSONArray data, final CallbackContext callbackContext){
684
+ private void getDeepLinkURL(JSONArray data, final CallbackContext callbackContext) {
615
685
  try {
616
- if(data != null){
686
+ if (data != null) {
617
687
  String url = "";
618
688
  String payload = (String) data.getJSONObject(0).get("pushPayload");
619
689
  JSONObject object = new JSONObject(payload);
@@ -635,7 +705,6 @@ public class AppICEPlugin extends CordovaPlugin {
635
705
  @Override
636
706
  public void run() {
637
707
  try {
638
- System.out.println("testing send notification "+bundle);
639
708
  if (callbackContext != null) {
640
709
  String pushPaylaod = bundle.getString("ai_content");
641
710
  PluginResult result = new PluginResult(PluginResult.Status.OK, pushPaylaod);
@@ -649,6 +718,7 @@ public class AppICEPlugin extends CordovaPlugin {
649
718
  }).start();
650
719
  }
651
720
 
721
+
652
722
  //==================================
653
723
  // SESSION SETTING APIS
654
724
  //==================================
@@ -874,7 +944,7 @@ public class AppICEPlugin extends CordovaPlugin {
874
944
 
875
945
  callbackContext.success();
876
946
  } catch (Throwable e) {
877
- Log.e(TAG, "trackScreens run: error ", e );
947
+ Log.e(TAG, "trackScreens run: error ", e);
878
948
  }
879
949
  }
880
950
  }).start();
@@ -882,4 +952,41 @@ public class AppICEPlugin extends CordovaPlugin {
882
952
  callbackContext.error(e.getMessage());
883
953
  }
884
954
  }
955
+
956
+ public static void storeTempData(String key, String value, Context context) {
957
+ try {
958
+ SharedPreferences sp = context.getSharedPreferences(TEMP_DB,
959
+ Context.MODE_PRIVATE);
960
+ SharedPreferences.Editor edit = sp.edit();
961
+ edit.putString(key, value);
962
+ edit.apply();
963
+ } catch (Throwable e) {
964
+ Log.e(TAG, "storeTempData: error ", e);
965
+ }
966
+ }
967
+
968
+ public static void resetTempData(String key, Context context) {
969
+ try {
970
+ SharedPreferences sp = context.getSharedPreferences(TEMP_DB,
971
+ Context.MODE_PRIVATE);
972
+ SharedPreferences.Editor edit = sp.edit();
973
+ edit.remove(key);
974
+ edit.apply();
975
+ } catch (Throwable e) {
976
+ Log.e(TAG, "resetTempData: error ", e);
977
+ }
978
+ }
979
+
980
+
981
+ public static String loadTempData(String key, Context ctx) {
982
+ String name = "";
983
+ try {
984
+ SharedPreferences sp = ctx.getSharedPreferences(TEMP_DB,
985
+ Context.MODE_PRIVATE);
986
+ name = sp.getString(key, "");
987
+ } catch (Throwable e) {
988
+ Log.e(TAG, "loadTempData: error ", e);
989
+ }
990
+ return name;
991
+ }
885
992
  }
@@ -0,0 +1,163 @@
1
+ package com.appice.cordova;
2
+
3
+ import android.app.PendingIntent;
4
+ import android.content.Context;
5
+ import android.content.Intent;
6
+ import android.content.pm.PackageManager;
7
+ import android.content.pm.ResolveInfo;
8
+ import android.os.Build;
9
+ import android.os.Bundle;
10
+ import android.util.Log;
11
+
12
+ import androidx.annotation.NonNull;
13
+
14
+ import com.google.firebase.messaging.RemoteMessage;
15
+ import com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPushIntentService;
16
+ import com.ibm.mobilefirstplatform.clientsdk.android.push.internal.MFPInternalPushMessage;
17
+
18
+ import org.json.JSONObject;
19
+
20
+ import java.util.List;
21
+ import java.util.Map;
22
+
23
+ import semusi.activitysdk.ContextSdk;
24
+ import semusi.context.utility.Utility;
25
+
26
+ /* import path for MainActivity */
27
+
28
+
29
+ /*=============================*/
30
+
31
+ public class AppICEPushHandler {
32
+ private static final String TAG = AppICEPushHandler.class.getSimpleName();
33
+ private static final String ACTION = "com.appice.campaignEvent";
34
+ private static final String PUSH_PAYLOAD = "ai_content";
35
+ private static final String MESSAGE = "message";
36
+ private static final String EXTERNAL_TYPE = "et";
37
+ private static final String DEEPLINK = "dl";
38
+ private static final String LANDING_PAGE = "lp";
39
+
40
+ public void onMessageReceived(RemoteMessage message, Context context) {
41
+ Map<String, String> data = message.getData();
42
+
43
+ if (isAppICEPush(message) && !checkIfPayloadHaveExternalUrl(message)) {
44
+ ContextSdk.handleAppICEPush(message, createPendingIntent(data.get(MESSAGE), context), deletePendingIntent(context), context);
45
+ } else if (isAppICEPush(message) && checkIfPayloadHaveExternalUrl(message)) {
46
+ ContextSdk.handleAppICEPush(message, context);
47
+ }
48
+ }
49
+
50
+ public void onNewToken(@NonNull String s, Context context) {
51
+ ContextSdk.checkIfDeviceRegisterToFCM(context);
52
+ }
53
+
54
+ public boolean isAppICEPush(RemoteMessage message) {
55
+ return ContextSdk.isAppICENotification(message);
56
+ }
57
+
58
+ public static PendingIntent createPendingIntent(String message, Context mContext) {
59
+ PendingIntent pendingIntent = null;
60
+ pendingIntent = internalCreatePendingIntent(message, mContext);
61
+ return pendingIntent;
62
+ }
63
+
64
+ public static PendingIntent deletePendingIntent(Context mContext) {
65
+ PendingIntent pendingIntent = null;
66
+ pendingIntent = internalDeletePendingIntent(mContext);
67
+ return pendingIntent;
68
+ }
69
+
70
+ //================================
71
+ // internal helper function
72
+ //================================
73
+ private static PendingIntent internalCreatePendingIntent(String message, Context mContext) {
74
+ try {
75
+ Class<?> launcherActivityClass = getLauncherActivityClass(mContext);
76
+ if (launcherActivityClass == null) {
77
+ // please use your launch activity here for example
78
+ // launcherActivityClass = MainActivity.class;
79
+ }
80
+ Intent clickIntent = new Intent(mContext, launcherActivityClass);
81
+ Bundle bundle = new Bundle();
82
+ final Intent newIntent = new Intent();
83
+ newIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
84
+ newIntent.setAction(ACTION);
85
+ clickIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
86
+ | Intent.FLAG_ACTIVITY_CLEAR_TOP);
87
+ newIntent.putExtra(PUSH_PAYLOAD, message);
88
+ bundle.putString(PUSH_PAYLOAD, message);
89
+ clickIntent.putExtras(bundle);
90
+
91
+ // Utility.sendEventToListener(mContext, newIntent);
92
+ return PendingIntent.getActivity(
93
+ mContext, 1, clickIntent, getPendingIntentFlags());
94
+ } catch (Throwable e) {
95
+ Log.e(TAG, "createPendingIntent: error ", e);
96
+ }
97
+ return null;
98
+ }
99
+
100
+ public static PendingIntent internalDeletePendingIntent(Context mContext) {
101
+ try {
102
+ // please use your launch activity here
103
+ Intent clickIntent = new Intent(mContext, CampaignCampsReceiver.class);
104
+ return PendingIntent.getBroadcast(mContext, 1, clickIntent,
105
+ getPendingIntentFlags());
106
+ } catch (Throwable t) {
107
+ Log.e(TAG, "deletePendingIntent: error ", t);
108
+ }
109
+ return null;
110
+ }
111
+
112
+ private static int getPendingIntentFlags() {
113
+ int flag;
114
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
115
+ flag = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE;
116
+ } else {
117
+ flag = PendingIntent.FLAG_UPDATE_CURRENT;
118
+ }
119
+
120
+ return flag;
121
+ }
122
+
123
+ private boolean checkIfPayloadHaveExternalUrl(RemoteMessage message) {
124
+ Map<String, String> map = message.getData();
125
+
126
+ try {
127
+ if (map != null) {
128
+ String messageStr = map.get(MESSAGE);
129
+ if (messageStr != null && messageStr.length() > 0) {
130
+ JSONObject messageObj = new JSONObject(messageStr);
131
+ String type = messageObj.optString(EXTERNAL_TYPE);
132
+ if (type.equalsIgnoreCase(DEEPLINK)) {
133
+ return false;
134
+ }
135
+ if (type.equalsIgnoreCase(LANDING_PAGE)) {
136
+ return true;
137
+ }
138
+ }
139
+ }
140
+ } catch (Throwable t) {
141
+ Log.e(TAG, "checkIfPayloadHaveExternalUrl: error ", t);
142
+ }
143
+ return false;
144
+ }
145
+
146
+ private static Class<?> getLauncherActivityClass(Context context) {
147
+ PackageManager packageManager = context.getPackageManager();
148
+ Intent intent = new Intent(Intent.ACTION_MAIN);
149
+ intent.addCategory(Intent.CATEGORY_LAUNCHER);
150
+ ResolveInfo resolveInfo = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
151
+
152
+ if (resolveInfo != null && resolveInfo.activityInfo != null) {
153
+ String className = resolveInfo.activityInfo.name;
154
+ try {
155
+ return Class.forName(className);
156
+ } catch (Throwable e) {
157
+ Log.e(TAG, "getLauncherActivityClass: error ", e);
158
+ }
159
+ }
160
+
161
+ return null;
162
+ }
163
+ }