Skip to content

Commit cc99490

Browse files
authored
Merge pull request #17 from Iterable/feature/ITBL-2245-android-image-push-notifications
Android notifications with images.
2 parents df4101d + 1ce1adc commit cc99490

File tree

4 files changed

+71
-10
lines changed

4 files changed

+71
-10
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ public final class IterableConstants {
5454
public static final String MESSAGING_PLATFORM_AMAZON = "ADM";
5555

5656
public static final String IS_GHOST_PUSH = "isGhostPush";
57-
public static final String ITERABLE_DATA_KEY = "itbl";
5857
public static final String ITERABLE_DATA_BODY = "body";
59-
public static final String ITERABLE_DATA_TITLE = "title";
58+
public static final String ITERABLE_DATA_KEY = "itbl";
59+
public static final String ITERABLE_DATA_PUSH_IMAGE = "attachment_url";
6060
public static final String ITERABLE_DATA_SOUND = "sound";
61+
public static final String ITERABLE_DATA_TITLE = "title";
6162

6263
//Device
6364
public static final String DEVICE_BRAND = "brand";

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ static void showFullScreenDialog(Context context, JSONObject dialogParameters, S
156156
verticalLayout.addView(imageView);
157157
try {
158158
Class picassoClass = Class.forName(IterableConstants.PICASSO_CLASS);
159-
if (picassoClass != null) {
160-
159+
String imageUrl = dialogParameters.optString(IterableConstants.ITERABLE_IN_APP_MAIN_IMAGE);
160+
if (picassoClass != null && !imageUrl.isEmpty()) {
161161
Picasso.
162162
with(context.getApplicationContext()).
163-
load(dialogParameters.optString(IterableConstants.ITERABLE_IN_APP_MAIN_IMAGE)).
163+
load(imageUrl).
164164
resize(dialogWidth, dialogHeight/2).
165165
centerInside().
166166
into(imageView);

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
import android.content.pm.PackageManager;
1010
import android.net.Uri;
1111
import android.os.Bundle;
12+
import android.os.Handler;
13+
import android.os.Looper;
1214
import android.support.v4.app.NotificationCompat;
15+
import android.widget.RemoteViews;
1316

14-
import java.util.Date;
17+
import com.squareup.picasso.Picasso;
1518

1619
/**
1720
*
@@ -20,13 +23,45 @@
2023
public class IterableNotification extends NotificationCompat.Builder {
2124
static final String TAG = "IterableNotification";
2225
private boolean isGhostPush;
26+
private String imageUrl;
2327
int requestCode;
2428
IterableNotificationData iterableNotificationData;
2529

2630
protected IterableNotification(Context context) {
2731
super(context);
2832
}
2933

34+
/**
35+
* Combine all of the options that have been set and return a new {@link Notification}
36+
* object.
37+
*/
38+
public Notification build() {
39+
final Notification notification = super.build();
40+
41+
final int iconId = android.R.id.icon;
42+
final int bigIconId = mContext.getResources().getIdentifier("android:id/big_picture", null, null);
43+
44+
Handler handler = new Handler(Looper.getMainLooper());
45+
handler.post(new Runnable() {
46+
@Override
47+
public void run() {
48+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN && imageUrl != null) {
49+
final RemoteViews bigContentView = notification.bigContentView;
50+
try {
51+
Class picassoClass = Class.forName(IterableConstants.PICASSO_CLASS);
52+
if (picassoClass != null) {
53+
Picasso.with(mContext).load(imageUrl).into(bigContentView, bigIconId, requestCode, notification);
54+
}
55+
} catch (ClassNotFoundException e) {
56+
IterableLogger.w(TAG, "ClassNotFoundException: Check that picasso is added " +
57+
"to the build dependencies", e);
58+
}
59+
}
60+
}
61+
});
62+
return notification;
63+
}
64+
3065
/**
3166
* Creates and returns an instance of IterableNotification.
3267
* @param context
@@ -40,13 +75,15 @@ public static IterableNotification createNotification(Context context, Bundle ex
4075
String notificationBody = null;
4176
String soundName = null;
4277
String messageId = null;
78+
String pushImage = null;
4379

4480
IterableNotification notificationBuilder = new IterableNotification(context);
4581

4682
if (extras.containsKey(IterableConstants.ITERABLE_DATA_KEY)) {
4783
applicationName = extras.getString(IterableConstants.ITERABLE_DATA_TITLE, applicationName);
4884
notificationBody = extras.getString(IterableConstants.ITERABLE_DATA_BODY);
4985
soundName = extras.getString(IterableConstants.ITERABLE_DATA_SOUND);
86+
pushImage = extras.getString(IterableConstants.ITERABLE_DATA_PUSH_IMAGE);
5087

5188
String iterableData = extras.getString(IterableConstants.ITERABLE_DATA_KEY);
5289
notificationBuilder.iterableNotificationData = new IterableNotificationData(iterableData);
@@ -66,10 +103,20 @@ public static IterableNotification createNotification(Context context, Bundle ex
66103
.setTicker(applicationName).setWhen(0)
67104
.setAutoCancel(true)
68105
.setContentTitle(applicationName)
69-
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
70106
.setPriority(Notification.PRIORITY_HIGH)
71107
.setContentText(notificationBody);
72108

109+
if (pushImage != null) {
110+
notificationBuilder.imageUrl = pushImage;
111+
notificationBuilder.setContentText(notificationBody)
112+
.setStyle(new NotificationCompat.BigPictureStyle()
113+
.setBigContentTitle(applicationName)
114+
.setSummaryText(notificationBody)
115+
);
116+
} else {
117+
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody));
118+
}
119+
73120
if (soundName != null) {
74121
//Removes the file type from the name
75122
String[] soundFile = soundName.split("\\.");
@@ -142,6 +189,7 @@ private static int getIconId(Context context) {
142189
try {
143190
ApplicationInfo info = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
144191
iconId = info.metaData.getInt(IterableConstants.NOTIFICATION_ICON_NAME, 0);
192+
IterableLogger.d(TAG, "iconID: "+ info.metaData.get(IterableConstants.NOTIFICATION_ICON_NAME));
145193
} catch (PackageManager.NameNotFoundException e) {
146194
e.printStackTrace();
147195
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.Context;
66
import android.content.Intent;
77
import android.content.pm.PackageManager;
8+
import android.os.AsyncTask;
89
import android.os.Bundle;
910

1011
/**
@@ -55,16 +56,27 @@ private void handlePushReceived(Context context, Intent intent) {
5556
try {
5657
mainClass = Class.forName(mainClassName);
5758
} catch (ClassNotFoundException e) {
58-
e.printStackTrace();
59+
IterableLogger.w(TAG, e.toString());
5960
}
6061

6162
IterableNotification notificationBuilder = IterableNotification.createNotification(
6263
appContext, intent.getExtras(), mainClass);
63-
64-
IterableNotification.postNotificationOnDevice(appContext, notificationBuilder);
64+
new IterableNotificationBuilder().execute(notificationBuilder);
6565
} else {
6666
IterableLogger.d(TAG, "Iterable ghost silent push received");
6767
}
6868
}
6969
}
70+
}
71+
72+
class IterableNotificationBuilder extends AsyncTask<IterableNotification, Void, Void> {
73+
74+
@Override
75+
protected Void doInBackground(IterableNotification... params) {
76+
if ( params != null && params[0] != null) {
77+
IterableNotification notificationBuilder = params[0];
78+
IterableNotification.postNotificationOnDevice(notificationBuilder.mContext, notificationBuilder);
79+
}
80+
return null;
81+
}
7082
}

0 commit comments

Comments
 (0)