Skip to content

Commit 82f8f05

Browse files
committed
Updates to notifications to appear multiline
Configurable notification color Configurable notification icon Refactored IterableNotification
1 parent fa96fd8 commit 82f8f05

File tree

4 files changed

+79
-75
lines changed

4 files changed

+79
-75
lines changed

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

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ public class IterableApi {
2121
//region Variables
2222
//---------------------------------------------------------------------------------------
2323
static final String TAG = "IterableApi";
24-
static final String NOTIFICATION_ICON_NAME = "iterable_notification_icon";
2524

26-
static IterableApi sharedInstance = null;
25+
static volatile IterableApi sharedInstance = new IterableApi();
2726

2827
private Context _applicationContext;
2928
private String _apiKey;
@@ -37,6 +36,9 @@ public class IterableApi {
3736

3837
//region Constructor
3938
//---------------------------------------------------------------------------------------
39+
IterableApi(){
40+
}
41+
4042
IterableApi(Context context, String apiKey, String email){
4143
updateData(context, apiKey, email);
4244
}
@@ -145,13 +147,7 @@ public static IterableApi sharedInstanceWithApiKey(Context currentActivity, Stri
145147
public static IterableApi sharedInstanceWithApiKey(Context currentContext, String apiKey,
146148
String email, boolean debugMode)
147149
{
148-
Context applicationContext = currentContext.getApplicationContext();
149-
150-
if (sharedInstance == null) {
151-
sharedInstance = new IterableApi(applicationContext, apiKey, email);
152-
} else {
153-
sharedInstance.updateData(applicationContext, apiKey, email);
154-
}
150+
sharedInstance.updateData(currentContext.getApplicationContext(), apiKey, email);
155151

156152
if (currentContext instanceof Activity) {
157153
Activity currentActivity = (Activity) currentContext;
@@ -346,16 +342,16 @@ public void disablePush(String iterableAppId, String gcmProjectNumber) {
346342

347343
//region Protected Fuctions
348344
//---------------------------------------------------------------------------------------
349-
static void setNotificationIcon(Context context, String iconName) {
350-
SharedPreferences sharedPref = context.getSharedPreferences(NOTIFICATION_ICON_NAME, Context.MODE_PRIVATE);
351-
SharedPreferences.Editor editor = sharedPref.edit();
352-
editor.putString(NOTIFICATION_ICON_NAME, iconName);
353-
editor.commit();
354-
}
345+
static void setNotificationIcon(Context context, String iconName) {
346+
SharedPreferences sharedPref = context.getSharedPreferences(IterableConstants.NOTIFICATION_ICON_NAME, Context.MODE_PRIVATE);
347+
SharedPreferences.Editor editor = sharedPref.edit();
348+
editor.putString(IterableConstants.NOTIFICATION_ICON_NAME, iconName);
349+
editor.commit();
350+
}
355351

356352
static String getNotificationIcon(Context context) {
357-
SharedPreferences sharedPref = context.getSharedPreferences(NOTIFICATION_ICON_NAME, Context.MODE_PRIVATE);
358-
String iconName = sharedPref.getString(NOTIFICATION_ICON_NAME, "");
353+
SharedPreferences sharedPref = context.getSharedPreferences(IterableConstants.NOTIFICATION_ICON_NAME, Context.MODE_PRIVATE);
354+
String iconName = sharedPref.getString(IterableConstants.NOTIFICATION_ICON_NAME, "");
359355
return iconName;
360356
}
361357

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@ public final class IterableConstants {
5252

5353
public static final String INSTANCE_ID_CLASS = "com.google.android.gms.iid.InstanceID";
5454
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";
5557
}

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

Lines changed: 63 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import android.app.PendingIntent;
66
import android.content.Context;
77
import android.content.Intent;
8-
import android.graphics.BitmapFactory;
8+
import android.content.pm.ApplicationInfo;
9+
import android.content.pm.PackageManager;
910
import android.os.Bundle;
10-
//import android.support.v7.app.NotificationCompat;
1111
import android.support.v4.app.NotificationCompat;
1212

1313
import java.util.Date;
@@ -17,6 +17,7 @@
1717
* Created by David Truong [email protected]
1818
*/
1919
public class IterableNotification extends NotificationCompat.Builder {
20+
static final String TAG = "IterableNotification";
2021
private boolean isGhostPush;
2122

2223
protected IterableNotification(Context context) {
@@ -28,10 +29,9 @@ protected IterableNotification(Context context) {
2829
* @param context
2930
* @param extras
3031
* @param classToOpen
31-
* @param icon
3232
* @return Returns null if the intent comes from an Iterable ghostPush
3333
*/
34-
public static IterableNotification createNotification(Context context, Bundle extras, Class classToOpen, int icon) {
34+
public static IterableNotification createNotification(Context context, Bundle extras, Class classToOpen) {
3535
int stringId = context.getApplicationInfo().labelRes;
3636
String applicationName = context.getString(stringId);
3737
String notificationBody = null;
@@ -48,51 +48,32 @@ public static IterableNotification createNotification(Context context, Bundle ex
4848
PendingIntent notificationClickedIntent = PendingIntent.getActivity(context, 0,
4949
mainIntentWithExtras, PendingIntent.FLAG_UPDATE_CURRENT);
5050

51-
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
51+
IterableNotification mBuilder = new IterableNotification(
5252
context);
53-
Notification notification = mBuilder.setSmallIcon(icon).setTicker(applicationName).setWhen(0)
53+
mBuilder
54+
.setDefaults(Notification.DEFAULT_SOUND)
55+
.setSmallIcon(getIconId(context))
5456
.setAutoCancel(true)
5557
.setContentTitle(applicationName)
5658
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
57-
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), icon))
58-
.setContentText(notificationBody).build();
59-
60-
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
61-
notificationManager.notify(99999, notification);
62-
63-
// NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
64-
// context);
65-
// IterableNotification notification = mBuilder.setSmallIcon(icon).setTicker(applicationName).setWhen(0)
66-
// .setAutoCancel(true)
67-
// .setContentTitle(applicationName)
68-
// .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
69-
// .setSmallIcon(icon)
70-
// .setContentText(notificationBody).build();
71-
72-
73-
IterableNotification notificationBuilder = new IterableNotification(context);
74-
notificationBuilder
75-
// .setSmallIcon(icon)
76-
// .setContentTitle(applicationName)
77-
// .setContentText(notificationBody)
78-
// .setStyle(new NotificationCompat.BigTextStyle()
79-
// .bigText(notificationBody))
80-
// .setAutoCancel(true);
81-
82-
.setTicker(applicationName).setWhen(0)
83-
.setAutoCancel(true)
84-
.setContentTitle(applicationName)
85-
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
86-
// .setContentIntent(resultPendingIntent)
87-
// .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
88-
.setSmallIcon(icon)
59+
.setPriority(Notification.PRIORITY_HIGH)
8960
.setContentText(notificationBody);
9061

91-
notificationBuilder.setContentIntent(notificationClickedIntent);
62+
try {
63+
ApplicationInfo info = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
64+
mBuilder.setColor(info.metaData.getInt(IterableConstants.NOTIFICATION_COLOR));
65+
} catch (PackageManager.NameNotFoundException e) {
66+
e.printStackTrace();
67+
}
9268

93-
notificationBuilder.isGhostPush = IterableHelper.isGhostPush(extras);
69+
PackageManager pm = context.getPackageManager();
70+
if (pm.checkPermission("android.permission.VIBRATE", context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
71+
mBuilder.setDefaults(Notification.DEFAULT_ALL);
72+
} else {
73+
mBuilder.setVibrate(null);
74+
}
9475

95-
return notificationBuilder;
76+
return mBuilder;
9677
}
9778

9879
/**
@@ -110,7 +91,47 @@ public static void postNotificationOnDevice(Context context, IterableNotificatio
11091
long dateInMilli = new Date().getTime();
11192
int notifID = (int) (dateInMilli % Integer.MAX_VALUE);
11293

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

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,8 @@ private void handlePushReceived(Context context, Intent intent) {
4949
e.printStackTrace();
5050
}
5151

52-
int resourceIconId = appContext.getResources().getIdentifier(
53-
IterableApi.getNotificationIcon(context),
54-
IterableConstants.ICON_FOLDER_IDENTIFIER,
55-
appContext.getPackageName());
56-
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");
62-
iconId = appContext.getApplicationInfo().icon;
63-
} else {
64-
IterableLogger.w(TAG, "No Notification Icon defined - push notifications will not be displayed");
65-
}
66-
6752
IterableNotification notificationBuilder = IterableNotification.createNotification(
68-
appContext, intent.getExtras(), mainClass, iconId);
53+
appContext, intent.getExtras(), mainClass);
6954

7055
IterableNotification.postNotificationOnDevice(appContext, notificationBuilder);
7156
}

0 commit comments

Comments
 (0)