Skip to content

Commit e5883f5

Browse files
authored
Merge pull request #4 from Iterable/development
Debug mode / customer requests / code cleanup
2 parents a652385 + 14e8226 commit e5883f5

File tree

8 files changed

+392
-232
lines changed

8 files changed

+392
-232
lines changed

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

Lines changed: 234 additions & 164 deletions
Large diffs are not rendered by default.

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

Lines changed: 7 additions & 3 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-
42-
public static final String PUSH_APPID = "IterableAppId";
43-
public static final String PUSH_PROJECTID = "GCMProjectNumber";
41+
public static final String PUSH_APP_ID = "IterableAppId";
42+
public static final String PUSH_GCM_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,9 @@ 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";
55+
public static final String NOTIFICATION_ICON_NAME = "iterable_notification_icon";
56+
public static final String NOTIFICATION_COLOR = "iterable_notification_color";
5357
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
public static void d(String tag, String msg) {
11+
if (isDebugMode()) {
12+
Log.d(tag, msg);
13+
}
14+
}
15+
16+
public static void d(String tag, String msg, Throwable tr) {
17+
if (isDebugMode()) {
18+
Log.d(tag, msg, tr);
19+
}
20+
}
21+
22+
public static void w(String tag, String msg) {
23+
if (isDebugMode()) {
24+
Log.w(tag, msg);
25+
}
26+
}
27+
28+
public static void w(String tag, String msg, Throwable tr) {
29+
if (isDebugMode()) {
30+
Log.w(tag, msg, tr);
31+
}
32+
}
33+
34+
public static void e(String tag, String msg) {
35+
if (isDebugMode()) {
36+
Log.e(tag, msg);
37+
}
38+
}
39+
40+
public static void e(String tag, String msg, Throwable tr) {
41+
if (isDebugMode()) {
42+
Log.e(tag, msg, tr);
43+
}
44+
}
45+
46+
private static boolean isDebugMode() {
47+
return (IterableApi.sharedInstance != null) ? IterableApi.sharedInstance.getDebugMode() : false;
48+
}
49+
}

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

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.iterable.iterableapi;
22

3+
import android.app.Notification;
34
import android.app.NotificationManager;
45
import android.app.PendingIntent;
56
import android.content.Context;
67
import android.content.Intent;
8+
import android.content.pm.ApplicationInfo;
9+
import android.content.pm.PackageManager;
710
import android.os.Bundle;
8-
import android.support.v7.app.NotificationCompat;
11+
import android.support.v4.app.NotificationCompat;
912

1013
import java.util.Date;
1114

@@ -14,6 +17,7 @@
1417
* Created by David Truong [email protected]
1518
*/
1619
public class IterableNotification extends NotificationCompat.Builder {
20+
static final String TAG = "IterableNotification";
1721
private boolean isGhostPush;
1822

1923
protected IterableNotification(Context context) {
@@ -25,10 +29,9 @@ protected IterableNotification(Context context) {
2529
* @param context
2630
* @param extras
2731
* @param classToOpen
28-
* @param icon
2932
* @return Returns null if the intent comes from an Iterable ghostPush
3033
*/
31-
public static IterableNotification createNotification(Context context, Bundle extras, Class classToOpen, int icon) {
34+
public static IterableNotification createNotification(Context context, Bundle extras, Class classToOpen) {
3235
int stringId = context.getApplicationInfo().labelRes;
3336
String applicationName = context.getString(stringId);
3437
String notificationBody = null;
@@ -45,18 +48,36 @@ public static IterableNotification createNotification(Context context, Bundle ex
4548
PendingIntent notificationClickedIntent = PendingIntent.getActivity(context, 0,
4649
mainIntentWithExtras, PendingIntent.FLAG_UPDATE_CURRENT);
4750

48-
49-
50-
IterableNotification notificationBuilder = new IterableNotification(context);
51-
notificationBuilder.setSmallIcon(icon)
51+
IterableNotification notificationBuilder = new IterableNotification(
52+
context);
53+
notificationBuilder
54+
.setDefaults(Notification.DEFAULT_SOUND)
55+
.setSmallIcon(getIconId(context))
56+
.setTicker(applicationName).setWhen(0)
57+
.setAutoCancel(true)
5258
.setContentTitle(applicationName)
53-
.setContentText(notificationBody)
54-
.setAutoCancel(true);
59+
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
60+
.setPriority(Notification.PRIORITY_HIGH)
61+
.setContentText(notificationBody);
5562

5663
notificationBuilder.setContentIntent(notificationClickedIntent);
57-
5864
notificationBuilder.isGhostPush = IterableHelper.isGhostPush(extras);
5965

66+
67+
try {
68+
ApplicationInfo info = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
69+
notificationBuilder.setColor(info.metaData.getInt(IterableConstants.NOTIFICATION_COLOR));
70+
} catch (PackageManager.NameNotFoundException e) {
71+
e.printStackTrace();
72+
}
73+
74+
PackageManager pm = context.getPackageManager();
75+
if (pm.checkPermission("android.permission.VIBRATE", context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
76+
notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
77+
} else {
78+
notificationBuilder.setVibrate(null);
79+
}
80+
6081
return notificationBuilder;
6182
}
6283

@@ -78,4 +99,44 @@ public static void postNotificationOnDevice(Context context, IterableNotificatio
7899
mNotificationManager.notify(notifID, iterableNotification.build());
79100
}
80101
}
102+
103+
/**
104+
* Returns the iconId from potential resource locations
105+
* @param context
106+
* @return
107+
*/
108+
private static int getIconId(Context context) {
109+
int iconId = 0;
110+
111+
//Get the iconId set in the AndroidManifest.xml
112+
if (iconId == 0) {
113+
try {
114+
ApplicationInfo info = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
115+
iconId = info.metaData.getInt(IterableConstants.NOTIFICATION_ICON_NAME, 0);
116+
} catch (PackageManager.NameNotFoundException e) {
117+
e.printStackTrace();
118+
}
119+
}
120+
121+
//Get the iconId set in code
122+
if (iconId == 0) {
123+
iconId = context.getResources().getIdentifier(
124+
IterableApi.getNotificationIcon(context),
125+
IterableConstants.ICON_FOLDER_IDENTIFIER,
126+
context.getPackageName());
127+
}
128+
129+
//Get id from the default app settings
130+
if (iconId == 0) {
131+
if (context.getApplicationInfo().icon != 0) {
132+
IterableLogger.d(TAG, "No Notification Icon defined - defaulting to app icon");
133+
iconId = context.getApplicationInfo().icon;
134+
}
135+
else {
136+
IterableLogger.w(TAG, "No Notification Icon defined - push notifications will not be displayed");
137+
}
138+
}
139+
140+
return iconId;
141+
}
81142
}

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: 3 additions & 21 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) {
35-
String iterableAppId = intent.getStringExtra(IterableConstants.PUSH_APPID);
36-
String projectNumber = intent.getStringExtra(IterableConstants.PUSH_PROJECTID);
31+
String iterableAppId = intent.getStringExtra(IterableConstants.PUSH_APP_ID);
32+
String projectNumber = intent.getStringExtra(IterableConstants.PUSH_GCM_PROJECT_NUMBER);
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,22 +49,8 @@ private void handlePushReceived(Context context, Intent intent) {
5349
e.printStackTrace();
5450
}
5551

56-
int iconId = appContext.getResources().getIdentifier(
57-
IterableApi.getNotificationIcon(context),
58-
"drawable",
59-
appContext.getPackageName());
60-
61-
if (iconId == 0) {
62-
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-
}
68-
}
69-
7052
IterableNotification notificationBuilder = IterableNotification.createNotification(
71-
appContext, intent.getExtras(), mainClass, iconId);
53+
appContext, intent.getExtras(), mainClass);
7254

7355
IterableNotification.postNotificationOnDevice(appContext, notificationBuilder);
7456
}

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.iterable.iterableapi;
22

3+
import android.content.Context;
34
import android.os.AsyncTask;
4-
import android.util.Log;
55

66
import com.google.android.gms.gcm.GoogleCloudMessaging;
77
import com.google.android.gms.iid.InstanceID;
@@ -24,29 +24,30 @@ protected String doInBackground(IterableGCMRegistrationData... params) {
2424
iterableGCMRegistrationData = params[0];
2525

2626
if (iterableGCMRegistrationData.iterableAppId != null) {
27-
Class instanceIdClass = Class.forName("com.google.android.gms.iid.InstanceID");
27+
Class instanceIdClass = Class.forName(IterableConstants.INSTANCE_ID_CLASS);
2828
if (instanceIdClass != null) {
29-
InstanceID instanceID = InstanceID.getInstance(IterableApi.sharedInstance.getMainActivityContext());
29+
Context mainContext = IterableApi.sharedInstance.getMainActivityContext();
30+
if (mainContext != null) {
31+
InstanceID instanceID = InstanceID.getInstance(mainContext);
3032

31-
String idInstance = instanceID.getId();
32-
registrationToken = instanceID.getToken(iterableGCMRegistrationData.projectNumber,
33-
GoogleCloudMessaging.INSTANCE_ID_SCOPE);
34-
if (!registrationToken.isEmpty()) {
35-
IterableApi.sharedInstance.registerDeviceToken(iterableGCMRegistrationData.iterableAppId, registrationToken);
33+
String idInstance = instanceID.getId();
34+
registrationToken = instanceID.getToken(iterableGCMRegistrationData.projectNumber,
35+
GoogleCloudMessaging.INSTANCE_ID_SCOPE);
36+
if (!registrationToken.isEmpty()) {
37+
IterableApi.sharedInstance.registerDeviceToken(iterableGCMRegistrationData.iterableAppId, registrationToken);
38+
}
39+
} else {
40+
IterableLogger.e(TAG, "MainActivity Context is null");
3641
}
3742
}
3843
} else {
39-
Log.e("IterableGCM", "The IterableAppId has not been added to the AndroidManifest");
44+
IterableLogger.e("IterableGCM", "The IterableAppId has not been added to the AndroidManifest");
4045
}
4146
} catch (ClassNotFoundException e) {
42-
//Notes: If there is a ClassNotFoundException add
43-
// compile 'com.google.android.gms:play-services-gcm:7.5.0' (min version) to the gradle dependencies
44-
Log.e(TAG, "ClassNotFoundException: Check that play-services-gcm is added " +
45-
"to the build dependencies");
46-
e.printStackTrace();
47+
IterableLogger.e(TAG, "ClassNotFoundException: Check that play-services-gcm is added " +
48+
"to the build dependencies", e);
4749
} catch (IOException e) {
48-
e.printStackTrace();
49-
Log.e(TAG, "Invalid projectNumber");
50+
IterableLogger.e(TAG, "Invalid projectNumber", e);
5051
}
5152
return registrationToken;
5253
}

0 commit comments

Comments
 (0)