Fort Ash
Fort Ash

Reputation: 127

Ringing an alarm in android from foreground service

I'm developing an app where users can create routines and set alarms and reminders for these routines. The current workflow that I have set up is:

  1. In app an EXACT alarm is scheduled using the alarm manager.
  2. This sends a pending intent to a broadcast receiver.
  3. The broadcast receiver receives the broadcast, verifies that the alarm should indeed ring and starts a foreground service
  4. The foreground service displays a notification, rings some tune and vibrates the phone

My exact question is: Is this overkill?

A similar workflow is used when sending notifications. The broadcast receiver starts a foreground service that then creates a persistent notification. While the service is running although so briefly that it's not noticable it is displaying its own notification which gets removed when the service stops.

Did I overcomplicate things with the ForegroundService? Should all this work be done in the broadcast receiver?

Thanks for your help. Matic

Upvotes: 2

Views: 742

Answers (1)

thankyoussd
thankyoussd

Reputation: 2471

This isn't an overkill if you need to actually use your own media player/vibrator and control their behavior instead of relying on those provided by the system notification channels (which come with limitations such as the inability to make useful changes after creation, sounds being cancelled by other notifications, users making accidental changes, etc).

Attempting to run media players/vibrators from the broadcast receiver in the background will run into background task limitations and result in all kinds of unpredictable issues.

I really wish Android made the notification channel APIs more flexible for developers. They don't have to sacrifice user control to achieve this at all.

Upvotes: 0

Related Questions