Skip to content

Commit 58248a5

Browse files
authored
feat(push): Adds customizer for Notification.Builder. (#137)
* feat(push): Adds customizer for Notification.Builder.
1 parent fbfacee commit 58248a5

File tree

4 files changed

+373
-77
lines changed

4 files changed

+373
-77
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2018, Leanplum, Inc. All rights reserved.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
package com.leanplum;
22+
23+
import android.app.Notification;
24+
import android.os.Bundle;
25+
import android.support.v4.app.NotificationCompat;
26+
27+
/**
28+
* Implement LeanplumNotificationBuilderCustomizer to customize the appearance of notifications
29+
* to support 2 lines of text in BigPicture style push notification.
30+
*
31+
* @author Anna Orlova
32+
*/
33+
34+
public interface LeanplumNotificationBuilderCustomizer extends LeanplumPushNotificationCustomizer{
35+
/**
36+
* Implement this method to support 2 lines of text in BigPicture style push
37+
* notification and you already have a customizer. Note, that you still need to implement
38+
* {@link LeanplumPushNotificationCustomizer#customize(NotificationCompat.Builder, Bundle)}
39+
* to support BigText style on devices with API less than 16. Please call
40+
* {@link LeanplumPushService#setCustomizer(LeanplumNotificationBuilderCustomizer)}
41+
*
42+
* @param builder Notification.Builder for push notification.
43+
* @param notificationPayload Bundle notification payload.
44+
*/
45+
void customize(Notification.Builder builder, Bundle notificationPayload);
46+
}

AndroidSDKPush/src/main/java/com/leanplum/LeanplumNotificationHelper.java

Lines changed: 101 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class LeanplumNotificationHelper {
6060
private static final int BIGPICTURE_TEXT_TOP_PADDING = -14;
6161
private static final int BIGPICTURE_TEXT_SIZE = 14;
6262
private static final String LEANPLUM_DEFAULT_PUSH_ICON = "leanplum_default_push_icon";
63+
private static final int MAX_ONE_LINE_TEXT_LENGTH = 37;
6364

6465
/**
6566
* If notification channels are supported this method will try to create
@@ -166,7 +167,7 @@ static NotificationCompat.Builder getNotificationCompatBuilder(Context context,
166167
* @param message Push notification Bundle.
167168
* @return Notification.Builder or null.
168169
*/
169-
static Notification.Builder getNotificationBuilder(Context context, Bundle message) {
170+
private static Notification.Builder getNotificationBuilder(Context context, Bundle message) {
170171
Notification.Builder builder = null;
171172
// If we are targeting API 26, try to find supplied channel to post notification.
172173
if (BuildUtil.isNotificationChannelSupported(context)) {
@@ -195,6 +196,59 @@ static Notification.Builder getNotificationBuilder(Context context, Bundle messa
195196
return builder;
196197
}
197198

199+
/**
200+
* Gets NotificationCompat.Builder for provided parameters.
201+
*
202+
* @param context The application context.
203+
* @param message Push notification Bundle.
204+
* @param contentIntent PendingIntent.
205+
* @param title String with title for push notification.
206+
* @param messageText String with text for push notification.
207+
* @param bigPicture Bitmap for BigPictureStyle notification.
208+
* @param defaultNotificationIconResourceId int Resource id for default push notification icon.
209+
* @return NotificationCompat.Builder or null.
210+
*/
211+
static NotificationCompat.Builder getNotificationCompatBuilder(Context context, Bundle message,
212+
PendingIntent contentIntent, String title, final String messageText, Bitmap bigPicture,
213+
int defaultNotificationIconResourceId) {
214+
NotificationCompat.Builder notificationCompatBuilder =
215+
getNotificationCompatBuilder(context, message);
216+
if (notificationCompatBuilder == null) {
217+
return null;
218+
}
219+
220+
if (defaultNotificationIconResourceId == 0) {
221+
notificationCompatBuilder.setSmallIcon(context.getApplicationInfo().icon);
222+
} else {
223+
notificationCompatBuilder.setSmallIcon(defaultNotificationIconResourceId);
224+
}
225+
226+
notificationCompatBuilder.setContentTitle(title)
227+
.setStyle(new NotificationCompat.BigTextStyle()
228+
.bigText(messageText))
229+
.setContentText(messageText);
230+
231+
if (bigPicture != null) {
232+
notificationCompatBuilder.setStyle(new NotificationCompat.BigPictureStyle()
233+
.bigPicture(bigPicture)
234+
.setBigContentTitle(title)
235+
.setSummaryText(messageText));
236+
}
237+
238+
// Try to put a notification on top of the notification area. This method was deprecated in API
239+
// level 26. For API level 26 and above we must use setImportance(int) for each notification
240+
// channel, not for each notification message.
241+
if (Build.VERSION.SDK_INT >= 16 && !BuildUtil.isNotificationChannelSupported(context)) {
242+
//noinspection deprecation
243+
notificationCompatBuilder.setPriority(Notification.PRIORITY_MAX);
244+
}
245+
notificationCompatBuilder.setAutoCancel(true);
246+
notificationCompatBuilder.setContentIntent(contentIntent);
247+
248+
return notificationCompatBuilder;
249+
250+
}
251+
198252
/**
199253
* Gets Notification.Builder with 2 lines at BigPictureStyle notification text.
200254
*
@@ -215,56 +269,68 @@ static Notification.Builder getNotificationBuilder(Context context, Bundle messa
215269
}
216270
Notification.Builder notificationBuilder =
217271
getNotificationBuilder(context, message);
272+
if (notificationBuilder == null) {
273+
return null;
274+
}
218275
if (defaultNotificationIconResourceId == 0) {
219276
notificationBuilder.setSmallIcon(context.getApplicationInfo().icon);
220277
} else {
221278
notificationBuilder.setSmallIcon(defaultNotificationIconResourceId);
222279
}
223280
notificationBuilder.setContentTitle(title)
281+
.setStyle(new Notification.BigTextStyle()
282+
.bigText(messageText))
224283
.setContentText(messageText);
225-
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle() {
226-
@Override
227-
protected RemoteViews getStandardView(int layoutId) {
228-
RemoteViews remoteViews = super.getStandardView(layoutId);
229-
// Modifications of stanxdard push RemoteView.
230-
try {
231-
int id = Resources.getSystem().getIdentifier("text", "id", "android");
232-
remoteViews.setBoolean(id, "setSingleLine", false);
233-
remoteViews.setInt(id, "setLines", 2);
234-
if (Build.VERSION.SDK_INT < 23) {
235-
// Make text smaller.
236-
remoteViews.setViewPadding(id, 0, BIGPICTURE_TEXT_TOP_PADDING, 0, 0);
237-
remoteViews.setTextViewTextSize(id, TypedValue.COMPLEX_UNIT_SP, BIGPICTURE_TEXT_SIZE);
284+
if (bigPicture != null) {
285+
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle() {
286+
@Override
287+
protected RemoteViews getStandardView(int layoutId) {
288+
RemoteViews remoteViews = super.getStandardView(layoutId);
289+
if (messageText != null && messageText.length() >= MAX_ONE_LINE_TEXT_LENGTH) {
290+
// Modifications of standard push RemoteView.
291+
try {
292+
int id = Resources.getSystem().getIdentifier("text", "id", "android");
293+
remoteViews.setBoolean(id, "setSingleLine", false);
294+
remoteViews.setInt(id, "setLines", 2);
295+
if (Build.VERSION.SDK_INT < 23) {
296+
// Make text smaller.
297+
remoteViews.setViewPadding(id, 0, BIGPICTURE_TEXT_TOP_PADDING, 0, 0);
298+
remoteViews.setTextViewTextSize(id, TypedValue.COMPLEX_UNIT_SP, BIGPICTURE_TEXT_SIZE);
299+
}
300+
} catch (Throwable throwable) {
301+
Log.e("Cannot modify push notification layout.");
302+
}
238303
}
239-
} catch (Throwable throwable) {
240-
Log.e("Cannot modify push notification layout.");
304+
return remoteViews;
241305
}
242-
return remoteViews;
243-
}
244-
};
306+
};
245307

246-
bigPictureStyle.bigPicture(bigPicture)
247-
.setBigContentTitle(title)
248-
.setSummaryText(message.getString(Constants.Keys.PUSH_MESSAGE_TEXT));
249-
notificationBuilder.setStyle(bigPictureStyle);
308+
bigPictureStyle.bigPicture(bigPicture)
309+
.setBigContentTitle(title)
310+
.setSummaryText(message.getString(Constants.Keys.PUSH_MESSAGE_TEXT));
311+
notificationBuilder.setStyle(bigPictureStyle);
250312

251-
if (Build.VERSION.SDK_INT >= 24) {
252-
// By default we cannot reach getStandardView method on API>=24. I we call
253-
// createBigContentView, Android will call getStandardView method and we can get
254-
// modified RemoteView.
255-
try {
256-
RemoteViews remoteView = notificationBuilder.createBigContentView();
257-
if (remoteView != null) {
258-
// We need to set received RemoteView as a custom big content view.
259-
notificationBuilder.setCustomBigContentView(remoteView);
313+
if (Build.VERSION.SDK_INT >= 24) {
314+
// By default we cannot reach getStandardView method on API>=24. I we call
315+
// createBigContentView, Android will call getStandardView method and we can get
316+
// modified RemoteView.
317+
try {
318+
RemoteViews remoteView = notificationBuilder.createBigContentView();
319+
if (remoteView != null) {
320+
// We need to set received RemoteView as a custom big content view.
321+
notificationBuilder.setCustomBigContentView(remoteView);
322+
}
323+
} catch (Throwable t) {
324+
Log.e("Cannot modify push notification layout.", t);
260325
}
261-
} catch (Throwable t) {
262-
Log.e("Cannot modify push notification layout.", t);
263326
}
264327
}
265-
266328
notificationBuilder.setAutoCancel(true);
267329
notificationBuilder.setContentIntent(contentIntent);
330+
if (!BuildUtil.isNotificationChannelSupported(context)) {
331+
//noinspection deprecation
332+
notificationBuilder.setPriority(Notification.PRIORITY_MAX);
333+
}
268334
return notificationBuilder;
269335
}
270336

0 commit comments

Comments
 (0)