capacitor-plugins icon indicating copy to clipboard operation
capacitor-plugins copied to clipboard

Crash occured in @capacitor/local-notifications

Open merbin2012 opened this issue 7 months ago • 2 comments

This issue is occurred due to large data set into the scheduled notification, but this crash should controlled liked try...catch block or other method.

Image

Exception java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
  at com.getcapacitor.Bridge.lambda$callPluginMethod$0 (Bridge.java:846)
  at android.os.Handler.handleCallback (Handler.java:789)
  at android.os.Handler.dispatchMessage (Handler.java:98)
  at android.os.Looper.loop (Looper.java:164)
  at android.os.HandlerThread.run (HandlerThread.java:65)
Caused by java.lang.reflect.InvocationTargetException:
  at java.lang.reflect.Method.invoke
  at com.getcapacitor.PluginHandle.invoke (PluginHandle.java:138)
  at com.getcapacitor.Bridge.lambda$callPluginMethod$0 (Bridge.java:837)
Caused by java.lang.RuntimeException:
  at android.app.PendingIntent.getBroadcastAsUser (PendingIntent.java:586)
  at android.app.PendingIntent.getBroadcast (PendingIntent.java:562)
  at com.capacitorjs.plugins.localnotifications.LocalNotificationManager.triggerScheduledNotification (LocalNotificationManager.java:333)
  at com.capacitorjs.plugins.localnotifications.LocalNotificationManager.buildNotification (LocalNotificationManager.java:236)
  at com.capacitorjs.plugins.localnotifications.LocalNotificationManager.schedule (LocalNotificationManager.java:149)
  at com.capacitorjs.plugins.localnotifications.LocalNotificationsPlugin.schedule (LocalNotificationsPlugin.java:80)
Caused by android.os.TransactionTooLargeException:
  at android.os.BinderProxy.transactNative
  at android.os.BinderProxy.transact (Binder.java:761)
  at android.app.IActivityManager$Stub$Proxy.getIntentSender (IActivityManager.java:6040)
  at android.app.PendingIntent.getBroadcastAsUser (PendingIntent.java:579)

merbin2012 avatar Apr 26 '25 07:04 merbin2012

I got the same crash. This is probably a duplicate of #1998.

NNBnh avatar May 12 '25 06:05 NNBnh

I have a patched version of this plugin to allow a programmatic BigImage see this issue. I simply scaled down my image to make it lighter. --> return Bitmap.createScaledBitmap(bitmap, 128, 128, true);

diff --git a/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotification.java b/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotification.java
index 7463256d40cdf87580e2b7d20a29cf6f80de281a..559cd4c926b9aa28abf08570eeff6b611b7a927b 100644
--- a/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotification.java
+++ b/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotification.java
@@ -15,7 +15,7 @@ import java.util.List;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
-
+import android.net.Uri;
 /**
  * Local notification object mapped from json plugin
  */
@@ -358,6 +358,24 @@ public class LocalNotification {
             this.schedule != null && (this.schedule.getOn() != null || this.schedule.getAt() != null || this.schedule.getEvery() != null)
         );
     }
+    public Bitmap getBigImage(Context context) {
+        if (attachments != null && attachments.size() > 0) {
+            try {
+                LocalNotificationAttachment bp = attachments.get(0);
+                String bpUrl = bp.getUrl();
+                final AssetUtil assets = AssetUtil.getInstance(context);
+                Uri uri = assets.parse(bpUrl);
+                if (uri != Uri.EMPTY) {
+                    Bitmap bitmap = assets.getIconFromUri(uri);
+                    return Bitmap.createScaledBitmap(bitmap, 128, 128, true);
+                }
+            } catch (Exception e){
+                e.printStackTrace();
+            }
+        }
+        return null;
+    }
+
 
     @Override
     public String toString() {
diff --git a/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java b/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java
index 0b826a5464a14aca12b3c66df2a491d292b71e5b..3a87319a745c606e723282727865b075a2faa34c 100644
--- a/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java
+++ b/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java
@@ -12,6 +12,7 @@ import android.graphics.Color;
 import android.media.AudioAttributes;
 import android.net.Uri;
 import android.os.Build;
+import android.graphics.Bitmap;
 import android.os.Bundle;
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
@@ -181,7 +182,10 @@ public class LocalNotificationManager {
                     .setSummaryText(localNotification.getSummaryText())
             );
         }
-
+        if (localNotification.getAttachments() != null) {
+            Bitmap bitmap = localNotification.getBigImage(context);
+            mBuilder.setLargeIcon(Bitmap.createScaledBitmap(bitmap, 64, 64, true));
+        }
         if (localNotification.getInboxList() != null) {
             NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
             for (String line : localNotification.getInboxList()) {
@@ -215,7 +219,6 @@ public class LocalNotificationManager {
         mBuilder.setOnlyAlertOnce(true);
 
         mBuilder.setSmallIcon(localNotification.getSmallIcon(context, getDefaultSmallIcon(context)));
-        mBuilder.setLargeIcon(localNotification.getLargeIcon(context));
 
         String iconColor = localNotification.getIconColor(config.getString("iconColor"));
         if (iconColor != null) {
diff --git a/capacitor/src/main/java/com/getcapacitor/plugin/util/AssetUtil.java b/capacitor/src/main/java/com/getcapacitor/plugin/util/AssetUtil.java
index 3a7043bb5b517e373ddb28a425dde08bf289d339..3e9c937a5c070820b9d7fe1dee10e703ba6017ce 100644
--- a/capacitor/src/main/java/com/getcapacitor/plugin/util/AssetUtil.java
+++ b/capacitor/src/main/java/com/getcapacitor/plugin/util/AssetUtil.java
@@ -101,7 +101,7 @@ public final class AssetUtil {
      * @return URI pointing to the given path.
      */
     private Uri getUriFromAsset(String path) {
-        String resPath = path.replaceFirst("file:/", "www").replaceFirst("\\?.*$", "");
+        String resPath = path.replaceFirst("file:/", "public").replaceFirst("\\?.*$", "");
         String fileName = resPath.substring(resPath.lastIndexOf('/') + 1);
         File file = getTmpFile(fileName);
 
@@ -255,8 +255,10 @@ public final class AssetUtil {
      * @param uri Internal image URI
      */
     public Bitmap getIconFromUri(Uri uri) throws IOException {
-        InputStream input = context.getContentResolver().openInputStream(uri);
-        return BitmapFactory.decodeStream(input);
+        InputStream stream = context.getContentResolver().openInputStream(uri);
+        BitmapFactory.Options options = new BitmapFactory.Options();
+        options.inPreferredConfig = Bitmap.Config.RGB_565;
+        return BitmapFactory.decodeStream(stream, null, options);
     }
 
     /**
@@ -323,7 +325,7 @@ public final class AssetUtil {
      */
     private Uri getUriFromFile(File file) {
         try {
-            String authority = context.getPackageName() + ".provider";
+            String authority = context.getPackageName() + ".fileprovider";
             return FileProvider.getUriForFile(context, authority, file);
         } catch (IllegalArgumentException e) {
             Logger.error("File not supported by provider", e);

Ratatinator97 avatar May 21 '25 14:05 Ratatinator97