5
5
import android .content .Intent ;
6
6
import android .content .SharedPreferences ;
7
7
import android .os .Bundle ;
8
- import android .util .Log ;
9
8
10
9
import org .json .JSONException ;
11
10
import org .json .JSONObject ;
@@ -28,6 +27,7 @@ public class IterableApi {
28
27
private String _apiKey ;
29
28
private String _email ;
30
29
30
+ private boolean _debugMode ;
31
31
private Bundle _payloadData ;
32
32
private IterableNotificationData _notificationData ;
33
33
@@ -41,24 +41,98 @@ private IterableApi(Context context, String apiKey, String email){
41
41
* @param currentActivity The current activity
42
42
* @return stored instance of IterableApi
43
43
*/
44
- public static IterableApi sharedInstanceWithApiKey (Activity currentActivity , String apiKey , String email )
44
+ public static IterableApi sharedInstanceWithApiKey (Activity currentActivity , String apiKey ,
45
+ String email )
45
46
{
46
- Context applicationContext = currentActivity .getApplicationContext ();
47
+ return sharedInstanceWithApiKey (currentActivity , apiKey , email , false );
48
+ }
49
+
50
+ /**
51
+ * Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
52
+ * Should be called whenever the app is opened.
53
+ * Allows the IterableApi to be intialized with debugging enabled
54
+ * @param currentActivity The current activity
55
+ * @return stored instance of IterableApi
56
+ */
57
+ public static IterableApi sharedInstanceWithApiKey (Activity currentActivity , String apiKey ,
58
+ String email , boolean debugMode )
59
+ {
60
+ return sharedInstanceWithApiKey ((Context ) currentActivity , apiKey , email , debugMode );
61
+ }
62
+
63
+ /**
64
+ * Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
65
+ * Should be called whenever the app is opened.
66
+ * @param currentActivity The current activity
67
+ * @return stored instance of IterableApi
68
+ */
69
+ public static IterableApi sharedInstanceWithApiKey (Context currentActivity , String apiKey ,
70
+ String email )
71
+ {
72
+ return sharedInstanceWithApiKey (currentActivity , apiKey , email , false );
73
+ }
74
+
75
+ /**
76
+ * Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
77
+ * Should be called whenever the app is opened.
78
+ * Allows the IterableApi to be intialized with debugging enabled
79
+ * @param currentContext The current context
80
+ * @return stored instance of IterableApi
81
+ */
82
+ public static IterableApi sharedInstanceWithApiKey (Context currentContext , String apiKey ,
83
+ String email , boolean debugMode )
84
+ {
85
+ Context applicationContext = currentContext .getApplicationContext ();
47
86
48
- if (sharedInstance == null )
49
- {
87
+ if (sharedInstance == null ) {
50
88
sharedInstance = new IterableApi (applicationContext , apiKey , email );
51
89
} else {
52
90
sharedInstance .updateData (applicationContext , apiKey , email );
53
91
}
54
92
55
- Intent calledIntent = currentActivity .getIntent ();
56
- sharedInstance .setPayloadData (calledIntent );
57
- sharedInstance .tryTrackNotifOpen (calledIntent );
93
+ if (currentContext instanceof Activity ) {
94
+ Activity currentActivity = (Activity ) currentContext ;
95
+ Intent calledIntent = currentActivity .getIntent ();
96
+ if (isIterableIntent (calledIntent )) {
97
+ sharedInstance .setPayloadData (calledIntent );
98
+ sharedInstance .tryTrackNotifOpen (calledIntent );
99
+ }
100
+ } else {
101
+ IterableLogger .w (TAG , "Notification Opens will not be tracked: " +
102
+ "sharedInstanceWithApiKey called with a Context that is not an instance of Activity. " +
103
+ "Pass in an Activity to IterableApi.sharedInstanceWithApiKey to enable open tracking" +
104
+ "or call onNewIntent when a new Intent is received." );
105
+ }
106
+
107
+ sharedInstance .setDebugMode (debugMode );
58
108
59
109
return sharedInstance ;
60
110
}
61
111
112
+ public static void onNewIntent (Intent intent ) {
113
+ if (sharedInstance == null ) {
114
+ IterableLogger .w (TAG , "sharedInstanceWithApiKey needs to be called before onNewIntent " +
115
+ "can be called" );
116
+ return ;
117
+ } else if (isIterableIntent (intent )) {
118
+ sharedInstance .setPayloadData (intent );
119
+ sharedInstance .tryTrackNotifOpen (intent );
120
+ } else {
121
+ IterableLogger .d (TAG , "onNewIntent not triggered by an Iterable notification" );
122
+ }
123
+
124
+ }
125
+
126
+ public static boolean isIterableIntent (Intent intent ) {
127
+ if (intent != null ) {
128
+ Bundle extras = intent .getExtras ();
129
+ if (extras != null && extras .containsKey (IterableConstants .ITERABLE_DATA_KEY )) {
130
+ return true ;
131
+ }
132
+ }
133
+ return false ;
134
+ }
135
+
62
136
private void updateData (Context context , String apiKey , String email ) {
63
137
this ._applicationContext = context ;
64
138
this ._apiKey = apiKey ;
@@ -69,6 +143,14 @@ protected Context getMainActivityContext() {
69
143
return _applicationContext ;
70
144
}
71
145
146
+ protected void setDebugMode (boolean debugMode ) {
147
+ _debugMode = debugMode ;
148
+ }
149
+
150
+ protected boolean getDebugMode () {
151
+ return _debugMode ;
152
+ }
153
+
72
154
/**
73
155
* Sets the icon to be displayed in notifications.
74
156
* The icon name should match the resource name stored in the /res/drawable directory.
@@ -95,18 +177,18 @@ protected static String getNotificationIcon(Context context) {
95
177
* Automatically generates a GCM token and registers it with Iterable.
96
178
* @param iterableAppId The applicationId of the Iterable Push Integration
97
179
* - https://app.iterable.com/integrations/mobilePush
98
- * @param gcmProjectId The Google Project Number
180
+ * @param gcmProjectNumber The Google Project Number
99
181
* - https://console.developers.google.com/iam-admin/settings
100
182
*/
101
- public void registerForPush (String iterableAppId , String gcmProjectId ) {
102
- registerForPush (iterableAppId , gcmProjectId , false );
183
+ public void registerForPush (String iterableAppId , double gcmProjectNumber ) {
184
+ registerForPush (iterableAppId , gcmProjectNumber , false );
103
185
}
104
186
105
- protected void registerForPush (String iterableAppId , String gcmProjectId , boolean disableAfterRegistration ) {
187
+ protected void registerForPush (String iterableAppId , double gcmProjectNumber , boolean disableAfterRegistration ) {
106
188
Intent pushRegistrationIntent = new Intent (_applicationContext , IterablePushReceiver .class );
107
189
pushRegistrationIntent .setAction (IterableConstants .ACTION_PUSH_REGISTRATION );
108
190
pushRegistrationIntent .putExtra (IterableConstants .PUSH_APPID , iterableAppId );
109
- pushRegistrationIntent .putExtra (IterableConstants .PUSH_PROJECTID , gcmProjectId );
191
+ pushRegistrationIntent .putExtra (IterableConstants .PUSH_PROJECT_NUMBER , gcmProjectNumber );
110
192
pushRegistrationIntent .putExtra (IterableConstants .PUSH_DISABLE_AFTER_REGISTRATION , disableAfterRegistration );
111
193
_applicationContext .sendBroadcast (pushRegistrationIntent );
112
194
}
@@ -310,7 +392,7 @@ public void updateUser(JSONObject dataFields) {
310
392
sendRequest (IterableConstants .ENDPOINT_UPDATEUSER , requestJSON );
311
393
}
312
394
313
- public void disablePush (String iterableAppId , String gcmProjectId ) {
395
+ public void disablePush (String iterableAppId , double gcmProjectId ) {
314
396
registerForPush (iterableAppId , gcmProjectId , true );
315
397
}
316
398
@@ -346,7 +428,7 @@ public String getPayloadData(String key) {
346
428
347
429
void setPayloadData (Intent intent ) {
348
430
Bundle extras = intent .getExtras ();
349
- if (extras != null && ! extras . isEmpty () && extras .containsKey (IterableConstants .ITERABLE_DATA_KEY )) {
431
+ if (extras != null && extras .containsKey (IterableConstants .ITERABLE_DATA_KEY )) {
350
432
setPayloadData (extras );
351
433
}
352
434
}
@@ -382,7 +464,7 @@ public int getCampaignId() {
382
464
return returnId ;
383
465
}
384
466
385
- public static void initDebugMode (String url ) {
467
+ public static void overrideURLEndpointPath (String url ) {
386
468
IterableRequest .overrideUrl = url ;
387
469
}
388
470
@@ -396,4 +478,49 @@ private void sendRequest(String resourcePath, JSONObject json) {
396
478
IterableApiRequest request = new IterableApiRequest (_apiKey , resourcePath , json .toString ());
397
479
new IterableRequest ().execute (request );
398
480
}
481
+
482
+ //region Deprecated Fuctions
483
+ //---------------------------------------------------------------------------------------
484
+
485
+ /**
486
+ * deprecated function
487
+ *
488
+ * @deprecated use {@link #registerForPush(String, double)} instead.
489
+ */
490
+ @ Deprecated
491
+ public void registerForPush (String iterableAppId , String gcmProjectId ) {
492
+ Double projectId = 0.0 ;
493
+
494
+ try {
495
+ projectId = Double .parseDouble (gcmProjectId );
496
+ } catch (NumberFormatException e ) {
497
+ IterableLogger .e (TAG , "gcmProjectID must be a double" , e );
498
+ return ;
499
+ }
500
+
501
+ registerForPush (iterableAppId , projectId , false );
502
+ }
503
+
504
+ /**
505
+ * deprecated function
506
+ *
507
+ * @deprecated use {@link #disablePush(String, double)} instead.
508
+ */
509
+ @ Deprecated
510
+ public void disablePush (String iterableAppId , String gcmProjectId ) {
511
+ Double projectId = 0.0 ;
512
+
513
+ try {
514
+ projectId = Double .parseDouble (gcmProjectId );
515
+ } catch (NumberFormatException e ) {
516
+ IterableLogger .e (TAG , "gcmProjectID must be a double" );
517
+ e .printStackTrace ();
518
+ return ;
519
+ }
520
+
521
+ registerForPush (iterableAppId , projectId , true );
522
+ }
523
+ //---------------------------------------------------------------------------------------
524
+ //endregion
525
+
399
526
}
0 commit comments