-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Random crashing behavior on Android, and crashing after system restarts all apps #2446
Comments
Using version 18.0.0 of flutter_local_notifications seems to prevent the random crashes that were also occurring with similar stack traces. Hopefully this also resolves the issues with starting the app after automatic app restarts. |
I'm still getting this on version 18.0.0. |
I'm also seeing this on the 17.2.3 version of the library, but I don't recall this error before, so it's possible that the crashing behavior could be due to a bug in my code. |
I believe that this is where the error is occuring: I wonder if the Also, another user of my app reports no crashing behavior, while I was seeing a lot of crashes for a while. |
Judging by the error, it's not saying the parameters are null, which would be So the intent itself is null. This is also indicated by A quick look at the Android Services docs shows that
You can get yourself into this situation with the plugin if you call @leaf-node, can you confirm that you're using this function with @EP-u-NW, @EPNW-Eric, since this was your PR, do you have any insight here? @MaikuB It seems if the plugin is going to endorse using Here is the relevant native code: Lines 15 to 26 in 4ef55a2
|
@Levi-Lesches thanks for the good issue analysis and brining this to my attention! I've already looked into it provide a write up tomorrow. |
My reason for needing a foreground service was audio recording and processing using dart. By starting a foreground service the process is treated as in foreground (even if not visible) and my dart code continued running. I think the best solution would be using As a sidenote, while I was thinking about the bug today I considered storing the start parameters in a static variable instead of an intent, but I then realized, that when the process is terminated by the system while under load, the variable would also lose its value, so this is not a solution. About wheter to remove the foreground feature or not this is my opinion: The fact that this bug surfaced shows that people are using the feature. Foreground services are tightly connected to notifications. Moving foreground services in to a new package would also mean that the new package would need to redo a lot of the notification related things this package already solves. If we want to keep the feature I can provide a PR until Friday (but feel free to do it yourself if you can get to it earlier). |
Right, to clarify, I don't think anyone wants to remove the feature. If anything, I was suggesting removing or at least deprecating the start sticky option. Seems like you agree that flag isn't inherently useful. In case we do keep it, what should we do on a null intent? Just return and quit? It seems the startup code you wrote is very dependent on there being an intent with some data, so I'm not sure what useful value the plugin has if there is no intent. If the solution is as simple as returning if null and deprecating the flag, I can take care of it. If there's something else that should be done, a PR would be great. |
Thanks for both of your help with this. : ) I am using https://github.com/AhsanSarwar45/flutter_boot_receiver to restart the app if the OS reboots, and it may eventually also handle restarting after app upgrades. I see that I can specify the service start type here: https://pub.dev/documentation/flutter_local_notifications/latest/flutter_local_notifications/AndroidFlutterLocalNotificationsPlugin/startForegroundService.html, but it's not yet clear to me how to update Edit: Oh, I see that the changes to the Here is the relevant section of my <service
android:name="com.dexterous.flutterlocalnotifications.ForegroundService"
android:exported="false"
android:stopWithTask="false"
android:foregroundServiceType="dataSync" />
<service
android:name="com.flux.flutter_boot_receiver.BootHandlerService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE" />
<receiver
android:enabled="true"
android:exported="true"
android:name="com.flux.flutter_boot_receiver.BootBroadcastReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.INTERNET"/> |
I should note that I tried wiping and uninstalling the app, and the crashing stopped. So hopefully I can reproduce the bug on a new build the next time GrapheneOS has an update for my phone. I wonder if there is an interplay between app optimizations after a phone reboot on the one hand, and the fact that |
I'm not sure if I would remove the flag, just because I don't see a usecase yet, doesn't mean some dev out there is actually benefiting from it... I'm not even sure if I would change something in native code. Yes, the app crashes but maybe thats a good thing: Imagine if it does not crash and just returns; users would not reach out to developers because from their perspective, the app would work "fine". Also developers investigating the reason why sometimes there seems to be no foreground service in their app would have no clue where to start since there is no kind logging. If we were about to do anything in native code, an idea might be to throw an even more explicit exception instead of a null pointer: throw new
IllegalStateException("The intent carrying the notification data is null, most likley beause the service start type was START_STICKY.\r\nConsider using START_REDELIVER_INTENT and see https://github.com/MaikuB/flutter_local_notifications/issues/2446 for more details."); Nethertheless we should we warn about @leaf-node Hm an interplay with optimizers is an interesting thought. I never actually came across a situation where a system under load terminates an app with a foreground service. Maybe optimizers are way more strict here and start "resource saving" earlyer. |
I tested an older version of my app with the app optimization, and persistent crashing returned. Sideloading an updated app with the recommended change, using Also, GrapheneOS does app optimization before the phone unlocks, and more after the screen is unlocked. It's this second round that highlighted problems for me. There have been no issues during optimization, but at the end of the second round, a notification appears that basically says to "tap here to automatically restart open apps", and that's when this bug was activated. |
@leaf-node thanks for the report! Could you do some testing with START_NOT_STICKY? Since we are currently discussing in the PR if we should remove the other start options I wonder if you are able to achieve your desired behavior with this? |
Yes, I can test out As for |
We could theoretically make start sticky work by just returning if the intent is null. But the question we ran into is, fundamentally, what work is being done that the notification should be reinstated? Other packages allow you to link Dart code to your foreground service, but this plugin just makes notification and that's it. So if the foreground service is sticky, the user will get notification again, but no actual logic will be running to justify it. For that reason we are thinking of removing the start type option entirely and only use start not sticky. In other words, say you're using this plugin to start a foreground service because you're downloading some files. The download is only happening in your dart code, not in the service. Well having a service makes it less likely your app will be killed, but if it is, and the system reinstates it because it's sticky, there's no way for the Java code to restart the download operation that's happening in Dart. There are other packages that actually let you pass a function when creating a foreground service, and that function is actually tied to the service such that it will be restarted as well. But that's a bit out of scope for this plugin now. See #2481, where I removed the option and update the documentation instead |
Using |
Describe the bug
App randomly crashes every few hours, or crashes when trying to start it after the app is stopped by the system following app optimization. My app is using a foreground service notification that at least some of the time may be persisting when the app and its background process gets closed by the user.
Tested on Android 15 with 34 target SDK with flutter_local_notifications 17.2.4.
To Reproduce
Crashing also occurs randomly every few hours. I started noticing this issue in the last few days, about a week after I upgraded dependencies for my app, including this module from version 17.2.3 to 17.2.4.
Expected behavior
The app shouldn't crash.
Sample code to reproduce the problem
I don't have source code available for this yet, hoping that it's an easy bug to solve. If not, please let me know, and I'll consider creating a basic app with this problem. Thanks!
** Crash Logs **
The text was updated successfully, but these errors were encountered: