Skip to content

Commit df72660

Browse files
committed
Working version of android notifications with images.
1 parent 1dbf7a8 commit df72660

File tree

4 files changed

+63
-60
lines changed

4 files changed

+63
-60
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
@@ -53,10 +53,11 @@ public final class IterableConstants {
5353
public static final String MESSAGING_PLATFORM_AMAZON = "ADM";
5454

5555
public static final String IS_GHOST_PUSH = "isGhostPush";
56-
public static final String ITERABLE_DATA_KEY = "itbl";
5756
public static final String ITERABLE_DATA_BODY = "body";
58-
public static final String ITERABLE_DATA_TITLE = "title";
57+
public static final String ITERABLE_DATA_KEY = "itbl";
58+
public static final String ITERABLE_DATA_PUSH_IMAGE = "pushImage";
5959
public static final String ITERABLE_DATA_SOUND = "sound";
60+
public static final String ITERABLE_DATA_TITLE = "title";
6061

6162
public static final String INSTANCE_ID_CLASS = "com.google.android.gms.iid.InstanceID";
6263
public static final String FIREBASE_MESSAGING_CLASS = "com.google.firebase.messaging.FirebaseMessaging";

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

Lines changed: 44 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,39 @@
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+
final RemoteViews contentView = notification.contentView;
49+
//Picasso.with(mContext).load(imageUrl).into(contentView, iconId, requestCode, notification);
50+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
51+
final RemoteViews bigContentView = notification.bigContentView;
52+
Picasso.with(mContext).load(imageUrl).into(bigContentView, bigIconId, requestCode, notification);
53+
}
54+
}
55+
});
56+
return notification;
57+
}
58+
3059
/**
3160
* Creates and returns an instance of IterableNotification.
3261
* @param context
@@ -40,13 +69,15 @@ public static IterableNotification createNotification(Context context, Bundle ex
4069
String notificationBody = null;
4170
String soundName = null;
4271
String messageId = null;
72+
String pushImage = null;
4373

4474
IterableNotification notificationBuilder = new IterableNotification(context);
4575

4676
if (extras.containsKey(IterableConstants.ITERABLE_DATA_KEY)) {
4777
applicationName = extras.getString(IterableConstants.ITERABLE_DATA_TITLE, applicationName);
4878
notificationBody = extras.getString(IterableConstants.ITERABLE_DATA_BODY);
4979
soundName = extras.getString(IterableConstants.ITERABLE_DATA_SOUND);
80+
pushImage = extras.getString(IterableConstants.ITERABLE_DATA_PUSH_IMAGE);
5081

5182
String iterableData = extras.getString(IterableConstants.ITERABLE_DATA_KEY);
5283
notificationBuilder.iterableNotificationData = new IterableNotificationData(iterableData);
@@ -66,10 +97,20 @@ public static IterableNotification createNotification(Context context, Bundle ex
6697
.setTicker(applicationName).setWhen(0)
6798
.setAutoCancel(true)
6899
.setContentTitle(applicationName)
69-
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
70100
.setPriority(Notification.PRIORITY_HIGH)
71101
.setContentText(notificationBody);
72102

103+
if (pushImage != null) {
104+
notificationBuilder.imageUrl = pushImage;
105+
notificationBuilder.setContentText(notificationBody)
106+
.setStyle(new NotificationCompat.BigPictureStyle()
107+
.setBigContentTitle(applicationName)
108+
.setSummaryText(notificationBody)
109+
);
110+
} else {
111+
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody));
112+
}
113+
73114
if (soundName != null) {
74115
//Removes the file type from the name
75116
String[] soundFile = soundName.split("\\.");
@@ -142,6 +183,7 @@ private static int getIconId(Context context) {
142183
try {
143184
ApplicationInfo info = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
144185
iconId = info.metaData.getInt(IterableConstants.NOTIFICATION_ICON_NAME, 0);
186+
IterableLogger.d(TAG, "iconID: "+ info.metaData.get(IterableConstants.NOTIFICATION_ICON_NAME));
145187
} catch (PackageManager.NameNotFoundException e) {
146188
e.printStackTrace();
147189
}

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
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
import android.util.Log;
11+
import android.widget.RemoteViews;
1012

1113
/**
1214
*
@@ -37,24 +39,6 @@ public void onReceive(Context context, Intent intent) {
3739
}
3840
}
3941

40-
// /**
41-
// * Handles the push registration data from the intent.
42-
// * @param context
43-
// * @param intent
44-
// */
45-
// private void handlePushRegistration(Context context, Intent intent) {
46-
// String iterableAppId = intent.getStringExtra(IterableConstants.PUSH_APP_ID);
47-
// String projectNumber = intent.getStringExtra(IterableConstants.PUSH_GCM_PROJECT_NUMBER);
48-
// boolean disablePush = intent.getBooleanExtra(IterableConstants.PUSH_DISABLE_AFTER_REGISTRATION, false);
49-
// String messagingPlatform = intent.getStringExtra(IterableConstants.MESSAGING_PUSH_SERVICE_PLATFORM);
50-
//
51-
//// IterablePushRegistrationData data = new IterablePushRegistrationData(iterableAppId, projectNumber, disablePush, messagingPlatform);
52-
//// new IterablePushRegistration().execute(data);
53-
// IterableApi sharedInstance = IterableApi.sharedInstance;
54-
// String token = sharedInstance.getDeviceToken(projectNumber, messagingPlatform);
55-
// sharedInstance.registerDeviceToken(iterableAppId, token);
56-
// }
57-
5842
/**
5943
* Handles receiving an incoming push notification from the intent.
6044
* @param context
@@ -74,16 +58,27 @@ private void handlePushReceived(Context context, Intent intent) {
7458
try {
7559
mainClass = Class.forName(mainClassName);
7660
} catch (ClassNotFoundException e) {
77-
e.printStackTrace();
61+
IterableLogger.w(TAG, e.toString());
7862
}
7963

8064
IterableNotification notificationBuilder = IterableNotification.createNotification(
8165
appContext, intent.getExtras(), mainClass);
82-
83-
IterableNotification.postNotificationOnDevice(appContext, notificationBuilder);
66+
new IterableNotificationBuilder().execute(notificationBuilder);
8467
} else {
8568
IterableLogger.d(TAG, "Iterable ghost silent push received");
8669
}
8770
}
8871
}
72+
}
73+
74+
class IterableNotificationBuilder extends AsyncTask<IterableNotification, Void, Void> {
75+
76+
@Override
77+
protected Void doInBackground(IterableNotification... params) {
78+
if ( params != null && params[0] != null) {
79+
IterableNotification notificationBuilder = params[0];
80+
IterableNotification.postNotificationOnDevice(notificationBuilder.mContext, notificationBuilder);
81+
}
82+
return null;
83+
}
8984
}

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

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,41 +39,6 @@ protected String doInBackground(IterablePushRegistrationData... params) {
3939
IterableApi.sharedInstance.disablePush(registrationToken);
4040
}
4141
}
42-
43-
// Context mainContext = IterableApi.sharedInstance.getMainActivityContext();
44-
// if (mainContext != null) {
45-
//
46-
// int fbUrl = mainContext.getResources().getIdentifier("firebase_database_url", "string", mainContext.getPackageName());
47-
// if (fbUrl != 0 && iterablePushRegistrationData.messagingPlatform.equalsIgnoreCase(IterableConstants.MESSAGING_PLATFORM_FIREBASE)) {
48-
// //FCM
49-
// Class fireBaseMessaging = Class.forName(IterableConstants.FIREBASE_MESSAGING_CLASS);
50-
// if (fireBaseMessaging != null) {
51-
// FirebaseInstanceId instanceID = FirebaseInstanceId.getInstance();
52-
// registrationToken = instanceID.getToken();
53-
// if (!registrationToken.isEmpty()) {
54-
// IterableLogger.e(TAG, "fcm token: " + registrationToken);
55-
// IterableApi.sharedInstance.registerDeviceToken(iterablePushRegistrationData.iterableAppId, registrationToken);
56-
// }
57-
// }
58-
// } else {
59-
// //GCM
60-
// Class instanceIdClass = Class.forName(IterableConstants.INSTANCE_ID_CLASS);
61-
// if (instanceIdClass != null) {
62-
// InstanceID instanceID = InstanceID.getInstance(mainContext);
63-
//
64-
// String idInstance = instanceID.getId();
65-
// registrationToken = instanceID.getToken(iterablePushRegistrationData.projectNumber,
66-
// GoogleCloudMessaging.INSTANCE_ID_SCOPE);
67-
// if (!registrationToken.isEmpty()) {
68-
// IterableLogger.e(TAG, "android projectNumber: " + iterablePushRegistrationData.projectNumber);
69-
// IterableLogger.e(TAG, "android token: " + registrationToken);
70-
// IterableApi.sharedInstance.registerDeviceToken(iterablePushRegistrationData.iterableAppId, registrationToken);
71-
// }
72-
// }
73-
// }
74-
// } else {
75-
// IterableLogger.e(TAG, "MainActivity Context is null");
76-
// }
7742
} else {
7843
IterableLogger.e("IterablePush", "The IterableAppId has not been added");
7944
}

0 commit comments

Comments
 (0)