cordova-plugin-local-notifications icon indicating copy to clipboard operation
cordova-plugin-local-notifications copied to clipboard

Error FLAG_INMUTABLE or FLAG_MUTABLE - Android 12

Open josuelmm opened this issue 2 years ago • 11 comments

Problem Central 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.

2022-03-29 23:53:11.893 23106-23183/es.nytelweb.winterhalter E/Capacitor: Serious error executing plugin java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:121) at com.getcapacitor.Bridge.lambda$callPluginMethod$0$Bridge(Bridge.java:592) at com.getcapacitor.-$$Lambda$Bridge$25SFHybyAQk7zS27hTVXh2p8tmw.run(Unknown Source:8) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:210) at android.os.Looper.loop(Looper.java:299) at android.os.HandlerThread.run(HandlerThread.java:67) Caused by: java.lang.IllegalArgumentException: es.nytelweb.winterhalter: 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.LocalNotificationManager.cancelTimerForNotification(LocalNotificationManager.java:402) at com.capacitorjs.plugins.localnotifications.LocalNotificationManager.schedule(LocalNotificationManager.java:149) at com.capacitorjs.plugins.localnotifications.LocalNotificationsPlugin.schedule(LocalNotificationsPlugin.java:57) ... 9 more 2022-03-29 23:53:11.894 23106-23183/es.nytelweb.winterhalter E/AndroidRuntime: FATAL EXCEPTION: CapacitorPlugins Process: es.nytelweb.winterhalter, PID: 23106 java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at com.getcapacitor.Bridge.lambda$callPluginMethod$0$Bridge(Bridge.java:601) at com.getcapacitor.-$$Lambda$Bridge$25SFHybyAQk7zS27hTVXh2p8tmw.run(Unknown Source:8) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:210) at android.os.Looper.loop(Looper.java:299) at android.os.HandlerThread.run(HandlerThread.java:67) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:121) at com.getcapacitor.Bridge.lambda$callPluginMethod$0$Bridge(Bridge.java:592) at com.getcapacitor.-$$Lambda$Bridge$25SFHybyAQk7zS27hTVXh2p8tmw.run(Unknown Source:8)  at android.os.Handler.handleCallback(Handler.java:938)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loopOnce(Looper.java:210)  at android.os.Looper.loop(Looper.java:299)  at android.os.HandlerThread.run(HandlerThread.java:67)  Caused by: java.lang.IllegalArgumentException: es.nytelweb.winterhalter: 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.LocalNotificationManager.cancelTimerForNotification(LocalNotificationManager.java:402) at com.capacitorjs.plugins.localnotifications.LocalNotificationManager.schedule(LocalNotificationManager.java:149) at com.capacitorjs.plugins.localnotifications.LocalNotificationsPlugin.schedule(LocalNotificationsPlugin.java:57) ... 9 more 2022-03-29 23:53:11.898 23106-23183/es.nytelweb.winterhalter D/OOMEventManagerFK: checkEventAndDumpForJE: 0 2022-03-29 23:53:11.966 23106-23183/es.nytelweb.winterhalter I/Process: Sending signal. PID: 23106 SIG: 9

josuelmm avatar Mar 30 '22 04:03 josuelmm

Same issue here, any one managed to solve this?

ahmad-alk avatar May 07 '22 10:05 ahmad-alk

In order for this solution to work for you, you must use Android API 31 or higher and Android SDK Tools 31.0.0 or higher because pending intents got updated. The main difference is the addition of PendingIntent.FLAG_MUTABLE and the fact that you must specify the mutability of each intent as stated here.

DavidTalevski avatar May 12 '22 10:05 DavidTalevski

Tried to fix it here: https://github.com/fquirin/cordova-plugin-local-notifications

fquirin avatar Jul 17 '22 13:07 fquirin

Hi @fquirin I copy-pasted the changes you made to the 3 files but it still crashed. Can you share steps on how to implement these changes?

shyamal890 avatar Aug 04 '22 16:08 shyamal890

@shyamal890 try installing the plugin from here: https://github.com/DavidTalevski/cordova-plugin-local-notifications

Here are the changes I've made: https://github.com/timkellypa/cordova-plugin-local-notifications/compare/master...DavidTalevski:cordova-plugin-local-notifications:master

DavidTalevski avatar Aug 04 '22 16:08 DavidTalevski

Thanks a lot @DavidTalevski I took inspiration from this blog - https://hashnode.com/post/how-to-use-ionic-cordova-plugin-with-custom-modification-ckqmltoaz07o2s2s1aw2f9lb0

Downloaded https://github.com/fquirin/cordova-plugin-local-notifications and followed the steps in the above blog.

The issue was solved.

Thank you so much @DavidTalevski and @fquirin

shyamal890 avatar Aug 04 '22 17:08 shyamal890

Hi @katzer @shyamal890 @DavidTalevski @fquirin I have merged all changes into one branch also added Android 13 runtime notification permission https://github.com/bhandaribhumin/cordova-plugin-local-notification-12

bhandaribhumin avatar Aug 05 '22 11:08 bhandaribhumin

I've seen there are 29 changed files with 1,436 additions and 404 deletions now 😅 . Is this still fully compatible with the original plugin @bhandaribhumin ? (no breaking changes ^^?) I'll probably give it a shot and see what happens :-).

Btw I was thinking about implementing the music-player notification features (aka MediaSession) as well. Did you try anything in that direction already? I'll probably not be able to implement this for iOS as well (if even possible).

fquirin avatar Aug 06 '22 07:08 fquirin

For some reason the notifications don't trigger at a specific time on Android 12 (foreground or background), but, for example: If it's 12:04 AM and I scheduled for 12:05 AM, it doesn't trigger, but if it's 12:05 AM and I schedule it for 12:05 AM it fires immediately. Any idea why? Is anyone facing something similar?

It works fine on another device I tested Android 7.1. My code:

this.PUSH_NOTIFICATION.schedule(reminders, () => { resolve(); });

Where reminders are made of, createReminder(id: number, title: string, message: string, hourOfRepeat?: string, minutesOfRepeat?: string): any { return { id: id, title: title, text: message, trigger: { every: { hour: hourOfRepeat, minute: minutesOfRepeat } } } }

LoneWolf36 avatar Aug 09 '22 14:08 LoneWolf36

Hi @fquirin Thanks for viewing the commit, it works but don't know why the particular time trigger is not working, I also tried debugging but the notification is not getting triggered, In android logcat I also see notification set at Next trigger at: Tue Aug 09 21:43:30 GMT+05:30 2022 this time but don't know the reason why it is not coming, that's what I'm looking for... Can you check where I made a mistake? No I haven't done music-player notification yet, but you are doing it, I can help.

EDIT: https://developer.android.com/reference/android/app/AlarmManager#setAlarmClock(android.app.AlarmManager.AlarmClockInfo,%20android.app.PendingIntent)

I think for mgr.setAlarmClock(info, pi); sdk 31+ require SCHEDULE_EXACT_ALARM permission :) <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

bhandaribhumin avatar Aug 09 '22 16:08 bhandaribhumin

In the past I had some issues with timed notifications because the Android system simply decided to kill the app due to some energy saving stuff. Many people got pretty annoyed by this "feature" so Google made some changes to Android to make it more consistent. If you add the permission (SCHEDULE_EXACT_ALARM) and use Android 11+ I think it should work on most devices out of the box (without "training" Android to treat the app properly) ^^.

About media session notification one could check: https://github.com/TimeTravelersHackedMe/cordova-plugin-media-session Maybe it makes sense to integrate some features from this plugin but it seems a bit more complex than I'd hoped and it looks like a lot of maintenance work is required to keep this feature alive :-/

fquirin avatar Aug 09 '22 19:08 fquirin