Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crashlytics error with useExactAlarm on version 14.1.1 #2033

Closed
mk-dev-1 opened this issue Jun 25, 2023 · 26 comments
Closed

Crashlytics error with useExactAlarm on version 14.1.1 #2033

mk-dev-1 opened this issue Jun 25, 2023 · 26 comments

Comments

@mk-dev-1
Copy link

We just updated the flutter_local_notification dependency in our app to the latest version 14.1.1 and are slowly starting to see error messages come in as shown below. We have previously been using version 13.0.0, therefore suspect that this issue may have been introduct with version 14.0.0.

Note that our app does not declare SCHEDULE_EXACT_ALARM but uses USE_EXACT_ALARM since on-time notifications are absolutely paramount.

Does anyone have any suggestions?

Fatal Exception: java.lang.RuntimeException: Unable to start receiver com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.dexterous.flutterlocalnotifications.models.ScheduleMode.useExactAlarm()' on a null object reference
       at android.app.ActivityThread.handleReceiver(ActivityThread.java:4458)
       at android.app.ActivityThread.access$1700(ActivityThread.java:261)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2165)
       at android.os.Handler.dispatchMessage(Handler.java:111)
       at android.os.Looper.loopOnce(Looper.java:238)
       at android.os.Looper.loop(Looper.java:357)
       at android.app.ActivityThread.main(ActivityThread.java:8090)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1026)

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.dexterous.flutterlocalnotifications.models.ScheduleMode.useExactAlarm()' on a null object reference
       at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setupAllowWhileIdleAlarm(:2)
       at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.scheduleNextRepeatingNotification(:48)
       at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.scheduleNextNotification(:20)
       at com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver.onReceive(:127)
       at android.app.ActivityThread.handleReceiver(ActivityThread.java:4449)
       at android.app.ActivityThread.access$1700(ActivityThread.java:261)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2165)
       at android.os.Handler.dispatchMessage(Handler.java:111)
       at android.os.Looper.loopOnce(Looper.java:238)
       at android.os.Looper.loop(Looper.java:357)
       at android.app.ActivityThread.main(ActivityThread.java:8090)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1026)
@MaikuB
Copy link
Owner

MaikuB commented Jun 25, 2023

I suspect there's a code issue with PR

@kaptnkoala are you able to help with this?

@MaikuB
Copy link
Owner

MaikuB commented Jun 26, 2023

@mk-dev-1 if you know how this could be as fixed as well then do share. I thought the PR had fallbacks to help migrate existing scheduled notifications. Did your app use to schedule notifications with allow while idle set to true?

@MaikuB
Copy link
Owner

MaikuB commented Jun 27, 2023

@mk-dev-1 following from above question, do you by chance have a code snippet of the Dart code being invoked? The stack trace seems to suggest this isn't happening for notifications scheduled prior to 14.0.0 but for newer notifications scheduled after app updated to use 14.x

@mk-dev-1
Copy link
Author

All notification scheduling is happening exclusively via this snippet:

return notificationManager!
        .zonedSchedule(
          id,
          title,
          subtitle,
          tz.TZDateTime.from(date, tz.local),
          NotificationDetails(
            android: _getAndroidNotificationChannel(type),
            iOS: DarwinNotificationDetails(
              categoryIdentifier: await darwinNotificationCategoryId(type),
            ),
          ),
          uiLocalNotificationDateInterpretation:
              UILocalNotificationDateInterpretation.absoluteTime,
          androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle, // <--- this has changed in the latest version
          payload: payload,
          matchDateTimeComponents:
              recurring ? DateTimeComponents.dayOfWeekAndTime : null,
        )
        .then((result) => id!);

Does that help in any way?

In terms of helping to solve the issue, I'm afraid all I can do is test and observe. When it comes to Android programming, I'm of no help....

@MaikuB
Copy link
Owner

MaikuB commented Jun 28, 2023

Afraid not. If anything, it's made it more confusing as the code snippet doesn't match the stack trace. Stack trace would indicate periodicallyShow() is used as that's where a repeat interval is specified for scheduleNextRepeatingNotification() to be called on Java side. I've not been able to reproduce this issue on a release build of the example app using either code that is similar to yours (a weekly notification) or one that can appear based on a repeat interval of, say, every minute. I doubt your app does this but does it happen to be using a fork of the plugin? At a loss as to how this is happening otherwise. The feature has been in since around end of April as well

@mk-dev-1
Copy link
Author

I can confirm with 100% certainty that this is the only place in the entire app that schedules any notification. As that snippet is used for creating weekly recurring notifications (see the matchDateTimeComponents part), could there be something in the background that would explain the stack trace?

In any case, app is in beta channel for now and we will keep monitoring and provide updates here...

@MaikuB
Copy link
Owner

MaikuB commented Jun 28, 2023

Should clarify I wasn't doubting what code you call, was trying to explain that I can't make sense of how that aligns with the stack trace. I'm aware it's a weekly notification and tried to schedule one myself via the example app to check. I don't anything that would update the notification to add in a repeat interval that's specific to the periodicallyShow() API. Tried to also schedule it, reboot before it would trigger and see if it would fail in the same way where a notification is shown and next one is scheduled. Wasn't able to reproduce the issue either. In case it was missed though, I was asking in my previous message on if you happened to be using are fork of the plugin. Are you? I doubt you are but safer to confirm

@MaikuB
Copy link
Owner

MaikuB commented Jun 30, 2023

Another to add, if you have confirmed you're using the plugin directly instead of a fork, are you able find a way to reproduce this issue e.g. reproduce this via the example app or link to a repo hosting a minimal app that can reproduce this?

@vishnunew
Copy link

vishnunew commented Jul 4, 2023

@MaikuB I have found the same issue on my app. The users who has the version of flutter_local_notifications: 13.0.0 and set the reminder for notification crash it after update app with flutter_local_notifications: 14.1.1 Here is the crash I got.

Exception java.lang.RuntimeException: Unable to start receiver com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.dexterous.flutterlocalnotifications.models.ScheduleMode.useAllowWhileIdle()' on a null object reference
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4315)
  at android.app.ActivityThread.-$$Nest$mhandleReceiver
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2152)
  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:7918)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:548)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:936)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.dexterous.flutterlocalnotifications.models.ScheduleMode.useAllowWhileIdle()' on a null object reference
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.repeatNotification (FlutterLocalNotificationsPlugin.java:658)
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.rescheduleNotifications (FlutterLocalNotificationsPlugin.java:205)
  at com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver.onReceive (ScheduledNotificationBootReceiver.java:20)
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4306)

Another crash

Exception java.lang.RuntimeException: Unable to start receiver com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.dexterous.flutterlocalnotifications.models.ScheduleMode.useExactAlarm()' on a null object reference
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4315)
  at android.app.ActivityThread.-$$Nest$mhandleReceiver
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2152)
  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:7918)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:548)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:936)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.dexterous.flutterlocalnotifications.models.ScheduleMode.useExactAlarm()' on a null object reference
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setupAllowWhileIdleAlarm (FlutterLocalNotificationsPlugin.java:696)
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.scheduleNextRepeatingNotification (FlutterLocalNotificationsPlugin.java:588)
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.scheduleNextNotification (FlutterLocalNotificationsPlugin.java:226)
  at com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver.onReceive (ScheduledNotificationReceiver.java:62)
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4306)

I have tried to reproduce but on local environment I am not able to reproduce but the crash are increasing.

This was my old code

return _plugin.zonedSchedule(
      notificationId,
      title,
      content,
      scheduledTime.nextInstanceOfTimeDailyOfDay(selectedWeekDay),
      platformChannelSpecifics,
      payload: payload,
      androidAllowWhileIdle: true,
      uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
      matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime,
    );

New code

return _plugin.zonedSchedule(
      notificationId,
      title,
      content,
      scheduledTime.nextInstanceOfTimeDailyOfDay(selectedWeekDay),
      platformChannelSpecifics,
      payload: payload,
      androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
      uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
      matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime,
    );

@MaikuB
Copy link
Owner

MaikuB commented Jul 4, 2023

I have tried to reproduce but on local environment I am not able to reproduce but the crash are increasing.

What steps did you take to do this? I wanted to see if this included reproducing the scenario of a scheduling a notification using version 13 and then update the app where version 14.1.1 is used.

Another thing is what are your Proguard rules?

@vishnunew
Copy link

What steps did you take to do this?

Device: Google Pixel 6 - Android 13

  1. Install old version of app that uses the version 13.
  2. Set reminder for to trigger after 5-10 minute on repeat everyday. - Permission already handled
  3. Update app that has version 14.1.1
  4. Wait for reminder to arrive (Checked below case)
    1. App is in background
    2. App is killed
    3. App is in foreground

Another thing is what are your Proguard rules?

I am referring this file.

Based on Crashlytics all the devices are in background while this crash

Screenshot 2023-07-05 at 9 12 31 AM

@MaikuB
Copy link
Owner

MaikuB commented Jul 5, 2023

Can you try seeing if you can reproduce the issue via the update scenario I mentioned earlier? I've tried to do this myself and can't reproduce the issue. Currently without a way to know how to reproduce this, I can't tell what the cause is and haven't been able to see how the code could cause this issue

@MaikuB
Copy link
Owner

MaikuB commented Jul 5, 2023

Sorry just looked at your response again @vishnunew where you actually said you tried this. So to double check, it means you attempted to reproduce the issue but wasn't able to successfully do this?

@vishnunew
Copy link

@MaikuB Yes I have tried this but not able to reproduce it. I will try with different device again will let you know.

@MaikuB
Copy link
Owner

MaikuB commented Jul 5, 2023

@mk-dev-1 @vishnunew actually I have a hypothesis...for your applications, do you have records on what is the earliest version of the plugin that you used? If so, was there ever a point where you used the deprecated showWeeklyAtDayAndTime() method? If not, what is the API you used to schedule these notifications before and would you have a code snippet for it?

@MaikuB
Copy link
Owner

MaikuB commented Jul 5, 2023

I believe I found a solution. Will try to get this out in 15.0.1 and try cherry pick the fix to a 14.1.2 release

@MaikuB
Copy link
Owner

MaikuB commented Jul 5, 2023

This is available now

@gibbsvjy007
Copy link

file

I am facing the same issue, @MaikuB I believe its resolved now.

@MaikuB
Copy link
Owner

MaikuB commented Jul 6, 2023

Thanks for sharing @gibbsvjy007

@mk-dev-1
Copy link
Author

mk-dev-1 commented Jul 6, 2023

@MaikuB Apologies that I was not able to contribute any further on this issue. So extra big thank you for looking into it and resolving it so quickly. Really, highly appreciated!

@MaikuB
Copy link
Owner

MaikuB commented Jul 6, 2023

@mk-dev-1 no worries. Have you been able to confirm that it's resolved through rolling out an updated build yet? Seeing if I should leave this issue open a bit longer

@MaikuB
Copy link
Owner

MaikuB commented Jul 18, 2023

As @gibbsvjy007 has confirmed this has fixed the problem and have not heard more about this, I'm going to close the issue

@MaikuB MaikuB closed this as completed Jul 18, 2023
@nohli
Copy link
Contributor

nohli commented Aug 21, 2023

Should this error be resolved with 15.1.0?

Exception java.lang.RuntimeException: Unable to start receiver com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.dexterous.flutterlocalnotifications.models.ScheduleMode.useAllowWhileIdle()' on a null object reference
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4315)
  at android.app.ActivityThread.-$$Nest$mhandleReceiver
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2152)
  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:7918)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:548)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:936)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.dexterous.flutterlocalnotifications.models.ScheduleMode.useAllowWhileIdle()' on a null object reference
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.repeatNotification
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.rescheduleNotifications
  at com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver.onReceive
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4306)

@MaikuB
Copy link
Owner

MaikuB commented Aug 21, 2023

@nohli yours is related but different. Do you mean to say you have this issue on 15.1.0? If so, are you able to open a new issue and provide as much details on how to reproduce this? Ideally this would include a link to a repo hosting a minimal app

@MaikuB
Copy link
Owner

MaikuB commented Aug 22, 2023

@nohli I believe I've found a fix and is referenced in #2076. This is based on the 16.0.1 prerelease at the moment. Pretty confident about this fix but if you have time to test it out then be good to hear back about it. When I got time I'll continue to publish the PR etc and will push out "hotfixes" too

@RuchiPurohit
Copy link
Contributor

I have updated to version 16.1.0, and am getting this same background crash. I am using zonedSchedule method. I have followed all the android setup guidelines in the readme section and have added the Proguard rules.

I am in a fix, can't think of anything else to do. Please help.

In the current version of my app with 16.1.0 version of this library, I have added USE_EXACT_ALARM permission as below. Have followed the android guidelines. [Previous version of my app was using 9.9.1 version of this library, and never faced any crashes.]

<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
    <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"
 	 android:maxSdkVersion="32" />

My zonedSchedule method looks like this

var timezonedTime = tz.TZDateTime.from(
    notificationTime,
    tz.local,
  );

  await flutterLocalNotificationsPlugin.zonedSchedule(
      getNotificationIdFromDBNotificationId(notificationId),
      title,
      body,
      timezonedTime,
      platformChannelSpecifics,
      uiLocalNotificationDateInterpretation:
          UILocalNotificationDateInterpretation.absoluteTime,
      androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle, // <- changed from androidAllowWhileIdle: true
      payload: habitId);

AndroidManifest.xml looks like this

<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver" android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
            </intent-filter>
        </receiver>
        <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" android:exported="false" />
        <receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ActionBroadcastReceiver" />

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.USE_EXACT_ALARM" />
    <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"
 	 android:maxSdkVersion="32" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
    <uses-permission android:name="com.google.android.gms.permission.AD_ID" tools:node="remove"/>

This is the backround crash I am getting.

Exception java.lang.RuntimeException: Unable to start receiver com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.dexterous.flutterlocalnotifications.models.ScheduleMode.useAllowWhileIdle()' on a null object reference
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4556)
  at android.app.ActivityThread.-$$Nest$mhandleReceiver
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2304)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loopOnce (Looper.java:240)
  at android.os.Looper.loop (Looper.java:351)
  at android.app.ActivityThread.main (ActivityThread.java:8377)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:584)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1013)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.dexterous.flutterlocalnotifications.models.ScheduleMode.useAllowWhileIdle()' on a null object reference
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setupAlarm
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.zonedScheduleNotification
  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.rescheduleNotifications
  at com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver.onReceive
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4540)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants