gdelataillade / alarm

A Flutter plugin to easily manage alarms on iOS and Android
https://pub.dev/packages/alarm
MIT License
132 stars 85 forks source link

Battery usage of the application #102

Open dev-chetan opened 1 year ago

dev-chetan commented 1 year ago

Thanks for your effort and great work. Alarms are ringing as expected on the iOS however I noticed huge jump in the battery usage of the application. If I am understanding correct than we are keeping application active in background and due to that battery usage is high so as a workaround can we send a silent notification just before the alarm time and make our application active. This way we will not have to keep our application active all the time, and we can save some battery. Please share your thoughts on this.

gdelataillade commented 1 year ago

Hi @dev-chetan,

I might be mistaken but I'm pretty sure that sending a silent notification before alarm rings won't wake the app.

Do you have more information on the amount of battery used ? You can see details in Settings > Battery.

dev-chetan commented 1 year ago

Sure, I can research with silent notification. I did checked in setting and its using around 60%-70% battery if we set alarm for morning as it will keep application active for whole night.

gdelataillade commented 1 year ago

The 70% you see in your iOS battery settings for an app means that app used 70% of the total battery consumed during that period, not that it used up 70% of your battery’s total capacity.

dev-chetan commented 1 year ago

Thanks for the quick response, When I don't set the Alarm application is hardly using 10% of battery for my app, For Alarm testing I didn't touch the application until the Alarm rang. it's a noticeable difference in battery usage between application with Alarm and without Alarm. 

Brandonfas commented 1 year ago

After full day of 100 --> 0% battery, I experienced 36% of iPhone 13 Pro battery from only background alarm app usage (19 hours total)

gdelataillade commented 1 year ago

Hi @Brandonfas @dev-chetan

Could you share a screenshot of this screen please ? It could really help.

If relevant, please share the last 10 days, last 24 hours and a specific hour.

IMG_3813

Brandonfas commented 1 year ago

@gdelataillade here are 2 screenshots from 24 hours, at the moment it’s difficult to do a longer test as battery life gets cut almost in half, reducing functionality of phone significantly

you’ll notice that 3 hours of watching videos on Instagram is still a significantly less battery than current alarm background fetch

IMG_9109 IMG_9108

gdelataillade commented 1 year ago

Hi @Brandonfas

Thanks for the screenshot. I'll think about a way to use less battery, but honestly I don't have any idea in mind...

gdelataillade commented 1 year ago

Everyone that has battery issues please share your battery usage.

synstin commented 11 months ago

@gdelataillade I have the same problem. When I look at battery utilization in my iOS settings, the apps that are using alarms are taking up over 60%.

My app does not require the alarm to be active all the time. Clicking on a specific [start button] will activate the alarm and allow me to activate the alarm on that page.

I want the alarm to be deactivated in the background again when the [end button] is pressed. Calling Alarm.stopAll() when I press the [end button] does not disable the alarm in the background and it is consuming battery.

synstin commented 11 months ago

IMG_1490 IMG_1491

@gdelataillade Screenshot of iOS battery usage. The alarm is constantly draining the battery even though it is not active.

In the second screenshot, you can see that the battery is draining at 4-5am, even though the smartphone is not being used and there is no active alarm.

Before I went to bed, the battery was at 97%, but when I checked it in the morning, it was down to less than 50%.

If there is no active alarm, we need a way to stop background tasks from draining the battery. For example, might need something like this

// When need to use alarms in app
Alarm.init()

// When alarms are no longer used in the app
Alarm.dispose()

In my opinion, this is very critical and users will not use the app if it is constantly draining battery like this.

I am using version 2.1.1 and tested on iPhone14 iOS17. I'll test and let you know if this is the same on Android.

gdelataillade commented 11 months ago

Hi @synstin

Thanks for your feedback.

Alarm background process is supposed to be automatically disposed when all alarms are stopped. Maybe there is a memory leak somewhere. I'll investigate this as soon as I find the time.

gdelataillade commented 11 months ago

I use a silent audio player to keep the alarm app active in the background, but only when an alarm is scheduled. This method is similar to what's used in other alarm apps like Alarmy. They seem to optimize battery life by playing the silent audio briefly, perhaps for 1 second every 10 seconds, instead of continuously. I plan to implement this intermittent playback method in my app as well to see if it helps save battery life.

synstin commented 11 months ago

@gdelataillade

Alarm.ringStream.stream.listen((_) => Alarm.stopAll());

I did stopAll() when I received the alarm completion event on the stream and the battery leak went away.

wayupdev commented 11 months ago

Hello everyone, I had the opportunity to test the package by taking a solution on the market as a comparison (alarmy). You can see on the screenshots that the consumption of the two alarms is identical. Unfortunately, Apple's current policy does not allow you to call an alarm with the application killed, and the need to keep an activity in the background is costly in terms of energy. Congratulations once again on all your hard work!

IMG_0030 IMG_0031 IMG_0032

Brandonfas commented 11 months ago

I use a silent audio player to keep the alarm app active in the background, but only when an alarm is scheduled. This method is similar to what's used in other alarm apps like Alarmy. They seem to optimize battery life by playing the silent audio briefly, perhaps for 1 second every 10 seconds, instead of continuously. I plan to implement this intermittent playback method in my app as well to see if it helps save battery life.

Would it be more optimal to check 1 second every 1 minute? Since alarms are set at a minute level anyway

That would be 1/60th of background audio playing compared to existing application battery life

If not, no problem. Just a thought!

gdelataillade commented 11 months ago

Hi @Brandonfas

Thanks for your suggestion to reduce the silent audio playback to 1 second per minute. It's an interesting idea, but I need to consider the limitations of my current approach. While it does consume a significant amount of battery, it's similar to methods used by established apps like Alarmy. This similarity suggests that under current iOS constraints, this might be one of the more viable options. As a solo developer continuously learning about iOS development, implementing major changes to background task management could lead to unexpected challenges, particularly given the complex nature of iOS's handling of such tasks. Although I'm aware that my method isn't the most battery-efficient, altering the playback frequency could risk the app being terminated prematurely by iOS. I'll stick with the existing method for now, but I'm definitely open to exploring more efficient solutions as I enhance my understanding of iOS development.

Contributions, research, feedback, or any help would be greatly appreciated and could significantly aid in improving the package in future releases.

no-1ne commented 11 months ago

Possibly give the user a choice to configure the silent audio playback, some apps need alarm at a minute level accuracy some need alarm at a sec level,

with this knob, developers will know why its eating battery and experiment with it(vs having to reach out to you)

Brandonfas commented 11 months ago

I use a silent audio player to keep the alarm app active in the background, but only when an alarm is scheduled. This method is similar to what's used in other alarm apps like Alarmy. They seem to optimize battery life by playing the silent audio briefly, perhaps for 1 second every 10 seconds, instead of continuously. I plan to implement this intermittent playback method in my app as well to see if it helps save battery life.

I've done some additional research into Alarmy and apparently they went from continuous to 1 second every 10 seconds in version 6.63.0 for the exact purpose of battery draining. Is this something that could be possibly implemented?

I love this application, only limitation seems to be battery drain in terms of long-term usage and user satisfaction. Thanks again for all of the hard work you've done!

gdelataillade commented 11 months ago

Hi @Brandonfas

Thank you for your research ! Could you provide the link where you read about the "1 second every 10 seconds" please ?

It's probably something I could implement in the plugin but it won't be easy to be honest. Do you see a significant difference of the battery usage between Alarmy and my alarm plugin ?

synstin commented 9 months ago

@gdelataillade I've suddenly noticed a huge increase in battery usage on IOS. I am using version 3.0.5.

gdelataillade commented 9 months ago

@synstin that's weird I did not make any changes on iOS on the background process part in the last weeks... could you share some of your battery usage ?

synstin commented 9 months ago

@gdelataillade It seems to happen after updating the version from 3.0.2 to 3.0.4 or 3.0.5. I'll make some more observations and get back to you. As always, thanks for the quick feedback.

nateshmbhat commented 2 months ago

any update on this ?

orkun1675 commented 1 month ago

Hi @gdelataillade, I'm building an app that tracks the users location and uses this plugin to ring an alarm. The alarms are typically rung within 1-2 hours of the app being backgrounded.

The app requests background location access and keeps the GPS on, fetching location updates even when in the background. Do you think this activity will be sufficient to keep the app live (not suspended/terminated)?

If so, would it be possible to provide a lever to turn off the silent audio player for apps that already run background tasks?

I'm happy to work on a PR if this sounds like a good idea.