|
34 | 34 | import android.graphics.drawable.Drawable; |
35 | 35 | import android.os.Build; |
36 | 36 | import android.os.Bundle; |
| 37 | +import android.support.annotation.Nullable; |
37 | 38 | import android.support.v4.app.NotificationCompat; |
38 | 39 | import android.text.TextUtils; |
39 | 40 | import android.util.TypedValue; |
|
42 | 43 | import com.leanplum.internal.Constants; |
43 | 44 | import com.leanplum.internal.JsonConverter; |
44 | 45 | import com.leanplum.internal.Log; |
| 46 | +import com.leanplum.internal.Util; |
| 47 | +import com.leanplum.utils.BitmapUtil; |
45 | 48 | import com.leanplum.utils.BuildUtil; |
46 | 49 |
|
47 | 50 | import java.util.IllegalFormatCodePointException; |
@@ -211,8 +214,13 @@ private static Notification.Builder getNotificationBuilder(Context context, Bund |
211 | 214 | static NotificationCompat.Builder getNotificationCompatBuilder(Context context, Bundle message, |
212 | 215 | PendingIntent contentIntent, String title, final String messageText, Bitmap bigPicture, |
213 | 216 | int defaultNotificationIconResourceId) { |
| 217 | + if (message == null) { |
| 218 | + return null; |
| 219 | + } |
| 220 | + |
214 | 221 | NotificationCompat.Builder notificationCompatBuilder = |
215 | 222 | getNotificationCompatBuilder(context, message); |
| 223 | + |
216 | 224 | if (notificationCompatBuilder == null) { |
217 | 225 | return null; |
218 | 226 | } |
@@ -246,91 +254,125 @@ static NotificationCompat.Builder getNotificationCompatBuilder(Context context, |
246 | 254 | notificationCompatBuilder.setContentIntent(contentIntent); |
247 | 255 |
|
248 | 256 | return notificationCompatBuilder; |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * Calls setStyle for notificationBuilder and it tries modify notification layout to support two |
| 261 | + * lines. |
| 262 | + * |
| 263 | + * @param notificationBuilder current Notification.Builder. |
| 264 | + * @param bigPictureStyle current Notification.BigPictureStyle. |
| 265 | + */ |
| 266 | + static void setModifiedBigPictureStyle(Notification.Builder notificationBuilder, |
| 267 | + Notification.Style bigPictureStyle) { |
| 268 | + if (Build.VERSION.SDK_INT < 16 || notificationBuilder == null || bigPictureStyle == null) { |
| 269 | + return; |
| 270 | + } |
| 271 | + try { |
| 272 | + notificationBuilder.setStyle(bigPictureStyle); |
| 273 | + |
| 274 | + if (Build.VERSION.SDK_INT >= 24) { |
| 275 | + // By default we cannot reach getStandardView method on API>=24. If we call |
| 276 | + // createBigContentView, Android will call getStandardView method and we can get |
| 277 | + // modified RemoteView. |
| 278 | + try { |
| 279 | + RemoteViews remoteView = notificationBuilder.createBigContentView(); |
| 280 | + if (remoteView != null) { |
| 281 | + // We need to set received RemoteView as a custom big content view. |
| 282 | + notificationBuilder.setCustomBigContentView(remoteView); |
| 283 | + } |
| 284 | + } catch (Throwable t) { |
| 285 | + Log.e("Cannot modify push notification layout.", t); |
| 286 | + } |
| 287 | + } |
| 288 | + } catch (Throwable t) { |
| 289 | + Log.e("Cannot set BigPicture style for push notification.", t); |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + /** |
| 294 | + * Gets Notification.BigPictureStyle with 2 lines text. |
| 295 | + * |
| 296 | + * @param message Push notification Bundle. |
| 297 | + * @param bigPicture Bitmap for BigPictureStyle notification. |
| 298 | + * @param title String with title for push notification. |
| 299 | + * @param messageText String with text for push notification. |
| 300 | + * @return Notification.BigPictureStyle or null. |
| 301 | + */ |
| 302 | + static Notification.BigPictureStyle getBigPictureStyle(Bundle message, Bitmap bigPicture, |
| 303 | + String title, final String messageText) { |
| 304 | + if (Build.VERSION.SDK_INT < 16 || message == null || bigPicture == null) { |
| 305 | + return null; |
| 306 | + } |
| 307 | + |
| 308 | + Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle() { |
| 309 | + @Override |
| 310 | + protected RemoteViews getStandardView(int layoutId) { |
| 311 | + RemoteViews remoteViews = super.getStandardView(layoutId); |
| 312 | + if (messageText != null && messageText.length() >= MAX_ONE_LINE_TEXT_LENGTH) { |
| 313 | + // Modifications of standard push RemoteView. |
| 314 | + try { |
| 315 | + int id = Resources.getSystem().getIdentifier("text", "id", "android"); |
| 316 | + remoteViews.setBoolean(id, "setSingleLine", false); |
| 317 | + remoteViews.setInt(id, "setLines", 2); |
| 318 | + if (Build.VERSION.SDK_INT < 23) { |
| 319 | + // Make text smaller. |
| 320 | + remoteViews.setViewPadding(id, 0, BIGPICTURE_TEXT_TOP_PADDING, 0, 0); |
| 321 | + remoteViews.setTextViewTextSize(id, TypedValue.COMPLEX_UNIT_SP, BIGPICTURE_TEXT_SIZE); |
| 322 | + } |
| 323 | + } catch (Throwable throwable) { |
| 324 | + Log.e("Cannot modify push notification layout."); |
| 325 | + } |
| 326 | + } |
| 327 | + return remoteViews; |
| 328 | + } |
| 329 | + }; |
| 330 | + |
| 331 | + bigPictureStyle.bigPicture(bigPicture) |
| 332 | + .setBigContentTitle(title) |
| 333 | + .setSummaryText(message.getString(Constants.Keys.PUSH_MESSAGE_TEXT)); |
249 | 334 |
|
| 335 | + return bigPictureStyle; |
250 | 336 | } |
251 | 337 |
|
252 | 338 | /** |
253 | | - * Gets Notification.Builder with 2 lines at BigPictureStyle notification text. |
| 339 | + * Gets Notification.Builder for provided parameters. |
254 | 340 | * |
255 | 341 | * @param context The application context. |
256 | 342 | * @param message Push notification Bundle. |
257 | 343 | * @param contentIntent PendingIntent. |
258 | 344 | * @param title String with title for push notification. |
259 | 345 | * @param messageText String with text for push notification. |
260 | | - * @param bigPicture Bitmap for BigPictureStyle notification. |
261 | 346 | * @param defaultNotificationIconResourceId int Resource id for default push notification icon. |
262 | 347 | * @return Notification.Builder or null. |
263 | 348 | */ |
264 | 349 | static Notification.Builder getNotificationBuilder(Context context, Bundle message, |
265 | | - PendingIntent contentIntent, String title, final String messageText, Bitmap bigPicture, |
| 350 | + PendingIntent contentIntent, String title, final String messageText, |
266 | 351 | int defaultNotificationIconResourceId) { |
267 | | - if (Build.VERSION.SDK_INT < 16) { |
268 | | - return null; |
269 | | - } |
270 | | - Notification.Builder notificationBuilder = |
271 | | - getNotificationBuilder(context, message); |
| 352 | + Notification.Builder notificationBuilder = getNotificationBuilder(context, message); |
272 | 353 | if (notificationBuilder == null) { |
273 | 354 | return null; |
274 | 355 | } |
| 356 | + |
275 | 357 | if (defaultNotificationIconResourceId == 0) { |
276 | 358 | notificationBuilder.setSmallIcon(context.getApplicationInfo().icon); |
277 | 359 | } else { |
278 | 360 | notificationBuilder.setSmallIcon(defaultNotificationIconResourceId); |
279 | 361 | } |
| 362 | + |
280 | 363 | notificationBuilder.setContentTitle(title) |
281 | | - .setStyle(new Notification.BigTextStyle() |
282 | | - .bigText(messageText)) |
283 | 364 | .setContentText(messageText); |
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 | | - } |
303 | | - } |
304 | | - return remoteViews; |
305 | | - } |
306 | | - }; |
307 | 365 |
|
308 | | - bigPictureStyle.bigPicture(bigPicture) |
309 | | - .setBigContentTitle(title) |
310 | | - .setSummaryText(message.getString(Constants.Keys.PUSH_MESSAGE_TEXT)); |
311 | | - notificationBuilder.setStyle(bigPictureStyle); |
312 | | - |
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); |
325 | | - } |
| 366 | + if (Build.VERSION.SDK_INT > 16) { |
| 367 | + notificationBuilder.setStyle(new Notification.BigTextStyle() |
| 368 | + .bigText(messageText)); |
| 369 | + if (!BuildUtil.isNotificationChannelSupported(context)) { |
| 370 | + //noinspection deprecation |
| 371 | + notificationBuilder.setPriority(Notification.PRIORITY_MAX); |
326 | 372 | } |
327 | 373 | } |
328 | 374 | notificationBuilder.setAutoCancel(true); |
329 | 375 | notificationBuilder.setContentIntent(contentIntent); |
330 | | - if (!BuildUtil.isNotificationChannelSupported(context)) { |
331 | | - //noinspection deprecation |
332 | | - notificationBuilder.setPriority(Notification.PRIORITY_MAX); |
333 | | - } |
334 | 376 | return notificationBuilder; |
335 | 377 | } |
336 | 378 |
|
@@ -463,4 +505,25 @@ static void startPushRegistrationService(Context context, String providerName) { |
463 | 505 | Log.e("Couldn't update " + providerName + " InstanceId token.", t); |
464 | 506 | } |
465 | 507 | } |
| 508 | + |
| 509 | + /** |
| 510 | + * Gets bitmap for BigPicture style push notification. |
| 511 | + * |
| 512 | + * @param context Current application context. |
| 513 | + * @param imageUrl String with url to image. |
| 514 | + * @return Scaled bitmap for push notification with big image or null. |
| 515 | + */ |
| 516 | + @Nullable |
| 517 | + static Bitmap getBigPictureBitmap(Context context, String imageUrl) { |
| 518 | + Bitmap bigPicture = null; |
| 519 | + // BigPictureStyle support requires API 16 and higher. |
| 520 | + if (!TextUtils.isEmpty(imageUrl) && Build.VERSION.SDK_INT >= 16) { |
| 521 | + bigPicture = BitmapUtil.getScaledBitmap(context, imageUrl); |
| 522 | + if (bigPicture == null) { |
| 523 | + Log.w(String.format("Image download failed for push notification with big picture. " + |
| 524 | + "No image will be included with the push notification. Image URL: %s.", imageUrl)); |
| 525 | + } |
| 526 | + } |
| 527 | + return bigPicture; |
| 528 | + } |
466 | 529 | } |
0 commit comments