From dc038e2c403da40acded3b77f147528ab139735e Mon Sep 17 00:00:00 2001 From: Harel M Date: Tue, 2 Aug 2022 23:51:25 +0300 Subject: [PATCH] Fixes Android 12 missing FLAG_IMMUTABLE (#98) --- .../marianhello/bgloc/sync/NotificationHelper.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/android/common/src/main/java/com/marianhello/bgloc/sync/NotificationHelper.java b/android/common/src/main/java/com/marianhello/bgloc/sync/NotificationHelper.java index 0959766ed..a80d4e4e5 100644 --- a/android/common/src/main/java/com/marianhello/bgloc/sync/NotificationHelper.java +++ b/android/common/src/main/java/com/marianhello/bgloc/sync/NotificationHelper.java @@ -62,7 +62,11 @@ public Notification getNotification(String title, String text, String largeIcon, builder.setSmallIcon(android.R.drawable.ic_menu_mylocation); } if (largeIcon != null && !largeIcon.isEmpty()) { - builder.setLargeIcon(BitmapFactory.decodeResource(appContext.getResources(), mResolver.getDrawable(largeIcon))); + int largeIconId = mResolver.getDrawable(largeIcon); + if (largeIconId == 0) { + logger.warn("The resource " + largeIcon + " was not found in the drawable folder. Please include it when building the app."); + } + builder.setLargeIcon(BitmapFactory.decodeResource(appContext.getResources(), largeIconId)); } if (color != null && !color.isEmpty()) { builder.setColor(this.parseNotificationIconColor(color)); @@ -74,7 +78,10 @@ public Notification getNotification(String title, String text, String largeIcon, if (launchIntent != null) { // NOTICE: testing apps might not have registered launch intent launchIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP); - PendingIntent contentIntent = PendingIntent.getActivity(appContext, 0, launchIntent, PendingIntent.FLAG_CANCEL_CURRENT); + int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S + ? PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE + : PendingIntent.FLAG_CANCEL_CURRENT; + PendingIntent contentIntent = PendingIntent.getActivity(appContext, 0, launchIntent, flags); builder.setContentIntent(contentIntent); }