@@ -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