ionic-team / capacitor-plugins

Official plugins for Capacitor ⚡️
518 stars 583 forks source link

Capacitor 4 Local Notifications ScheduleOn bug #1137

Closed youxiang-git closed 2 years ago

youxiang-git commented 2 years ago

Bug Report

Plugin(s)

Capacitor 4 Local Notifications - '@capacitor/local-notifications'

Capacitor Version

Latest Dependencies:

  @capacitor/cli: 4.0.1
  @capacitor/core: 4.0.1
  @capacitor/android: 4.0.1
  @capacitor/ios: 4.0.1

Installed Dependencies:

  @capacitor/ios: not installed
  @capacitor/cli: 4.0.1
  @capacitor/android: 4.0.1
  @capacitor/core: 4.0.1

[success] Android looking great! 👌

Platform(s)

Android API Level 31/32

Current Behavior

I'm trying to schedule a local notification at a specific interval using the cron-style ScheduleOn functionality built into Capacitor 4.0. However, upon successful delivery of the notification, the app crashes with the error attached in Additional Context. I've found a similar issue to this regarding Android API 31/32 saying that it has been fixed in Capacitor 4.0, but it seems to still exist when using ScheduleOn.

Expected Behavior

Notification should be delivered without crashing the app. Subsequent notifications should also be scheduled.

Code Reproduction

https://github.com/youxiang-git/ionic-local-notifs-bug.git

Other Technical Details

Android Studio Android Studio Chipmunk | 2021.2.1 Patch 2 Build #AI-212.5712.43.2112.8815526, built on July 10, 2022 Runtime version: 11.0.12+7-b1504.28-7817840 amd64 VM: OpenJDK 64-Bit Server VM by Oracle Corporation Windows 10 10.0 GC: G1 Young Generation, G1 Old Generation Memory: 1280M Cores: 12 Registry: external.system.auto.import.disabled=true

Additional Context

2022-08-18 10:50:43.820 8327-8327/io.ionic.starter E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.ionic.starter, PID: 8327
    java.lang.RuntimeException: Unable to start receiver com.capacitorjs.plugins.localnotifications.TimedNotificationPublisher: java.lang.IllegalArgumentException: io.ionic.starter: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
    Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:4357)
        at android.app.ActivityThread.access$1600(ActivityThread.java:256)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2101)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.IllegalArgumentException: io.ionic.starter: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
    Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
        at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
        at com.capacitorjs.plugins.localnotifications.TimedNotificationPublisher.rescheduleNotificationIfNeeded(TimedNotificationPublisher.java:52)
        at com.capacitorjs.plugins.localnotifications.TimedNotificationPublisher.onReceive(TimedNotificationPublisher.java:40)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:4348)
            ... 9 more
mpkasp commented 2 years ago

I had the same error, had to update TimedNotificationPublisher.java:

--- a/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/TimedNotificationPublisher.java
+++ b/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/TimedNotificationPublisher.java
@@ -49,7 +49,11 @@ public class TimedNotificationPublisher extends BroadcastReceiver {
             AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
             long trigger = date.nextTrigger(new Date());
             Intent clone = (Intent) intent.clone();
-            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, clone, PendingIntent.FLAG_CANCEL_CURRENT);
+            int flags = PendingIntent.FLAG_CANCEL_CURRENT;
+            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
+                flags = flags | PendingIntent.FLAG_MUTABLE;
+            }
+            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, clone, flags);
ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of the plugin, please create a new issue and ensure the template is fully filled out.