Skip to content

Commit 98d24c2

Browse files
committed
Adds in option to toggle logging for debugMode.
Changed sharedInstanceWithApiKey to be able to take in a more generic context rather than an Activity. Added in helper functions to handle passing in new Intents to track pushOpens (onNewIntent & isIterableIntent) Deprecated registerForPush/disablePush (String, String) in favor of (String, double) Code cleanup / review feedback
1 parent e6f8be5 commit 98d24c2

File tree

7 files changed

+222
-58
lines changed

7 files changed

+222
-58
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java

Lines changed: 143 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.content.Intent;
66
import android.content.SharedPreferences;
77
import android.os.Bundle;
8-
import android.util.Log;
98

109
import org.json.JSONException;
1110
import org.json.JSONObject;
@@ -28,6 +27,7 @@ public class IterableApi {
2827
private String _apiKey;
2928
private String _email;
3029

30+
private boolean _debugMode;
3131
private Bundle _payloadData;
3232
private IterableNotificationData _notificationData;
3333

@@ -41,24 +41,98 @@ private IterableApi(Context context, String apiKey, String email){
4141
* @param currentActivity The current activity
4242
* @return stored instance of IterableApi
4343
*/
44-
public static IterableApi sharedInstanceWithApiKey(Activity currentActivity, String apiKey, String email)
44+
public static IterableApi sharedInstanceWithApiKey(Activity currentActivity, String apiKey,
45+
String email)
4546
{
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();
4786

48-
if (sharedInstance == null)
49-
{
87+
if (sharedInstance == null) {
5088
sharedInstance = new IterableApi(applicationContext, apiKey, email);
5189
} else{
5290
sharedInstance.updateData(applicationContext, apiKey, email);
5391
}
5492

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);
58108

59109
return sharedInstance;
60110
}
61111

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+
62136
private void updateData(Context context, String apiKey, String email) {
63137
this._applicationContext = context;
64138
this._apiKey = apiKey;
@@ -69,6 +143,14 @@ protected Context getMainActivityContext() {
69143
return _applicationContext;
70144
}
71145

146+
protected void setDebugMode(boolean debugMode) {
147+
_debugMode = debugMode;
148+
}
149+
150+
protected boolean getDebugMode() {
151+
return _debugMode;
152+
}
153+
72154
/**
73155
* Sets the icon to be displayed in notifications.
74156
* The icon name should match the resource name stored in the /res/drawable directory.
@@ -95,18 +177,18 @@ protected static String getNotificationIcon(Context context) {
95177
* Automatically generates a GCM token and registers it with Iterable.
96178
* @param iterableAppId The applicationId of the Iterable Push Integration
97179
* - https://app.iterable.com/integrations/mobilePush
98-
* @param gcmProjectId The Google Project Number
180+
* @param gcmProjectNumber The Google Project Number
99181
* - https://console.developers.google.com/iam-admin/settings
100182
*/
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);
103185
}
104186

105-
protected void registerForPush(String iterableAppId, String gcmProjectId, boolean disableAfterRegistration) {
187+
protected void registerForPush(String iterableAppId, double gcmProjectNumber, boolean disableAfterRegistration) {
106188
Intent pushRegistrationIntent = new Intent(_applicationContext, IterablePushReceiver.class);
107189
pushRegistrationIntent.setAction(IterableConstants.ACTION_PUSH_REGISTRATION);
108190
pushRegistrationIntent.putExtra(IterableConstants.PUSH_APPID, iterableAppId);
109-
pushRegistrationIntent.putExtra(IterableConstants.PUSH_PROJECTID, gcmProjectId);
191+
pushRegistrationIntent.putExtra(IterableConstants.PUSH_PROJECT_NUMBER, gcmProjectNumber);
110192
pushRegistrationIntent.putExtra(IterableConstants.PUSH_DISABLE_AFTER_REGISTRATION, disableAfterRegistration);
111193
_applicationContext.sendBroadcast(pushRegistrationIntent);
112194
}
@@ -310,7 +392,7 @@ public void updateUser(JSONObject dataFields) {
310392
sendRequest(IterableConstants.ENDPOINT_UPDATEUSER, requestJSON);
311393
}
312394

313-
public void disablePush(String iterableAppId, String gcmProjectId) {
395+
public void disablePush(String iterableAppId, double gcmProjectId) {
314396
registerForPush(iterableAppId, gcmProjectId, true);
315397
}
316398

@@ -346,7 +428,7 @@ public String getPayloadData(String key) {
346428

347429
void setPayloadData(Intent intent) {
348430
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)) {
350432
setPayloadData(extras);
351433
}
352434
}
@@ -382,7 +464,7 @@ public int getCampaignId() {
382464
return returnId;
383465
}
384466

385-
public static void initDebugMode(String url) {
467+
public static void overrideURLEndpointPath(String url) {
386468
IterableRequest.overrideUrl = url;
387469
}
388470

@@ -396,4 +478,49 @@ private void sendRequest(String resourcePath, JSONObject json) {
396478
IterableApiRequest request = new IterableApiRequest(_apiKey, resourcePath, json.toString());
397479
new IterableRequest().execute(request);
398480
}
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+
399526
}

iterableapi/src/main/java/com/iterable/iterableapi/IterableConstants.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ public final class IterableConstants {
3838
public static final String ENDPOINT_UPDATEEMAIL = "users/updateEmail";
3939
public static final String ENDPOINT_UPDATEUSER = "users/update";
4040

41-
4241
public static final String PUSH_APPID = "IterableAppId";
43-
public static final String PUSH_PROJECTID = "GCMProjectNumber";
42+
public static final String PUSH_PROJECT_NUMBER = "GCMProjectNumber";
4443
public static final String PUSH_DISABLE_AFTER_REGISTRATION = "DisableAfterRegistration";
4544

4645
public static final String MESSAGING_PLATFORM_GOOGLE = "GCM";
@@ -50,4 +49,7 @@ public final class IterableConstants {
5049
public static final String ITERABLE_DATA_KEY = "itbl";
5150
public static final String ITERABLE_DATA_BODY = "body";
5251
public static final String ITERABLE_DATA_TITLE = "title";
52+
53+
public static final String INSTANCE_ID_CLASS = "com.google.android.gms.iid.InstanceID";
54+
public static final String ICON_FOLDER_IDENTIFIER = "drawable";
5355
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.iterable.iterableapi;
2+
3+
import android.util.Log;
4+
5+
/**
6+
* Created by David Truong [email protected].
7+
*/
8+
class IterableLogger {
9+
10+
11+
public static void d(String tag, String msg) {
12+
if (isDebugMode()) {
13+
Log.d(tag, msg);
14+
}
15+
}
16+
17+
public static void d(String tag, String msg, Throwable tr) {
18+
if (isDebugMode()) {
19+
Log.d(tag, msg, tr);
20+
}
21+
}
22+
23+
public static void w(String tag, String msg) {
24+
if (isDebugMode()) {
25+
Log.w(tag, msg);
26+
}
27+
}
28+
29+
public static void w(String tag, String msg, Throwable tr) {
30+
if (isDebugMode()) {
31+
Log.w(tag, msg, tr);
32+
}
33+
}
34+
35+
public static void e(String tag, String msg) {
36+
if (isDebugMode()) {
37+
Log.e(tag, msg);
38+
}
39+
}
40+
41+
public static void e(String tag, String msg, Throwable tr) {
42+
if (isDebugMode()) {
43+
Log.e(tag, msg, tr);
44+
}
45+
}
46+
47+
private static boolean isDebugMode() {
48+
return (IterableApi.sharedInstance != null) ? IterableApi.sharedInstance.getDebugMode() : false;
49+
}
50+
}

iterableapi/src/main/java/com/iterable/iterableapi/IterableNotificationData.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,23 @@ class IterableNotificationData {
2525
if (iterableJson.has(IterableConstants.IS_GHOST_PUSH)) {
2626
isGhostPush = iterableJson.getBoolean(IterableConstants.IS_GHOST_PUSH);
2727
}
28-
29-
//TODO: do we need to parse out any additional dataFields to pass to trackPushOpen?
3028
} catch (JSONException e) {
3129
e.printStackTrace();
3230
}
3331
}
3432

3533
public int getCampaignId()
3634
{
37-
//include validation, logic, logging or whatever you like here
3835
return this.campaignId;
3936
}
4037

4138
public int getTemplateId()
4239
{
43-
//include validation, logic, logging or whatever you like here
4440
return this.templateId;
4541
}
4642

4743
public boolean getIsGhostPush()
4844
{
49-
//include validation, logic, logging or whatever you like here
5045
return this.isGhostPush;
5146
}
5247
}

iterableapi/src/main/java/com/iterable/iterableapi/IterablePushReceiver.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.content.Context;
66
import android.content.Intent;
77
import android.content.pm.PackageManager;
8-
import android.util.Log;
98

109
/**
1110
*
@@ -16,7 +15,6 @@ public class IterablePushReceiver extends BroadcastReceiver{
1615
static final String TAG = "IterablePushReceiver";
1716

1817
private static final String ACTION_GCM_RECEIVE_INTENT = "com.google.android.c2dm.intent.RECEIVE";
19-
private static final String ACTION_GCM_REGISTRATION_INTENT = "com.google.android.c2dm.intent.REGISTRATION";
2018

2119
@Override
2220
public void onReceive(Context context, Intent intent) {
@@ -26,14 +24,12 @@ public void onReceive(Context context, Intent intent) {
2624
handlePushRegistration(context, intent);
2725
} else if (intentAction.equals(ACTION_GCM_RECEIVE_INTENT)) {
2826
handlePushReceived(context, intent);
29-
} else if (intentAction.equals(ACTION_GCM_REGISTRATION_INTENT)) {
30-
Log.d(TAG, "received GCM registration intent action");
3127
}
3228
}
3329

3430
private void handlePushRegistration(Context context, Intent intent) {
3531
String iterableAppId = intent.getStringExtra(IterableConstants.PUSH_APPID);
36-
String projectNumber = intent.getStringExtra(IterableConstants.PUSH_PROJECTID);
32+
double projectNumber = intent.getDoubleExtra(IterableConstants.PUSH_PROJECT_NUMBER, 0);
3733
boolean disablePush = intent.getBooleanExtra(IterableConstants.PUSH_DISABLE_AFTER_REGISTRATION, false);
3834
IterableGCMRegistrationData data = new IterableGCMRegistrationData(iterableAppId, projectNumber, disablePush);
3935
new IterablePushRegistrationGCM().execute(data);
@@ -53,18 +49,19 @@ private void handlePushReceived(Context context, Intent intent) {
5349
e.printStackTrace();
5450
}
5551

56-
int iconId = appContext.getResources().getIdentifier(
52+
int resourceIconId = appContext.getResources().getIdentifier(
5753
IterableApi.getNotificationIcon(context),
58-
"drawable",
54+
IterableConstants.ICON_FOLDER_IDENTIFIER,
5955
appContext.getPackageName());
6056

61-
if (iconId == 0) {
57+
int iconId = 0;
58+
if (resourceIconId != 0) {
59+
iconId = resourceIconId;
60+
} else if (appContext.getApplicationInfo().icon != 0) {
61+
IterableLogger.d(TAG, "No Notification Icon defined - defaulting to app icon");
6262
iconId = appContext.getApplicationInfo().icon;
63-
if (iconId != 0) {
64-
Log.d(TAG, "No Notification Icon defined - defaulting to app icon");
65-
} else {
66-
Log.w(TAG, "No Notification Icon defined - push notifications will not be displayed");
67-
}
63+
} else {
64+
IterableLogger.w(TAG, "No Notification Icon defined - push notifications will not be displayed");
6865
}
6966

7067
IterableNotification notificationBuilder = IterableNotification.createNotification(

0 commit comments

Comments
 (0)