wbreeze / ShipsClock

An iOS, SwiftUI clock for tracking watches on board a ship. Marks the day in periods of four hours.
https://wbreeze.github.io/ShipsClock
Apache License 2.0
2 stars 0 forks source link

Ring bell when application is in the background #1

Closed wbreeze closed 4 years ago

wbreeze commented 4 years ago

The bells are nice, but the application ought not need to be in the foreground, the currently active one, in order for them to play. They should play when the application is open, but in the background. They should play when the phone is "sleeping" (display turned off). They should not play when the phone is silenced with the silence switch.

wbreeze commented 4 years ago

Apple is understandably careful, even pecunious, with the resources made available by iOS and the development API's to applications for background use of resources-- CPU and memory yes, but mainly power. Evidence the Apple Worldwide Developer Conference 2019 (WWDC 2019) talk about "Advances in App Background Execution" in which is described a clamp-down of control in which Apple doles out resources to background applications pretty much when they think best to do so, and for previsualized use cases, such as phone calls, audio playback, and refresh of social media feeds.

Simulating a grandfather or cuckoo clock that chimes in your house, this is probably not high on their list, although a grandfather clock that chimes only when you're looking at it (application active in foreground) has to be considered a pretty useless grandfather clock.

I've tried the background API's from the above linked talk. Neither background refresh nor background processing tasks were invoked with any frequency at all. Asking for them no earlier than fifteen seconds, they were rarely invoked. No. The solution has to be something else. The Apple clock that comes installed somehow manages to keep itself up to date, even in the background, repainting its system icon. The alarm clock function works to play an alarm when the clock is not in the foreground.

There's some capability for "Handling Notifications and Notification-Related Actions". An app can be asked to be notified at a given time in order to play a sound or interact in a limited way with the user. There are notifications human interface guidelines. This looks promising.

wbreeze commented 4 years ago

The notification capability isn't working out. There is a notification with the sound of a bell. That much is good; however, it doesn't execute any bit of the application code in order to schedule the next bell ring. The actions, including the default dismissal action, that is the userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) function in the UNUserNotificationCenterDelegate, are only called when the user actually interacts with the notification.

What's needed is for the application to make a sound while in background, then schedule the next make a sound event.

wbreeze commented 4 years ago

Researching this on the Apple Development Forums, it seems, the suggestion is to have a server push a notification to the device in order to trigger the push notification response in the app. This is possible, but overkill for this application. It's just a chiming clock. Further, the application must function when it is disconnected from the world of wireless data, for example, at sea.

Here are some forum links where it's either said that this can't be done or can only be done with push notifications.

One poster in the last said, "No. That cannot be done on iOS. This has been discussed many many times here on these forums." The implication is that this has been hashed out, rejected and isn't welcome to be revisited.

wbreeze commented 4 years ago

Here we go. It's possible to add forty-eight notifications, one for each half hour of the day, that match the half hour with the right bell and repeat each day. Coming back to foreground, the application can cancel them. As a bonus, without title or body, they don't display in the notification center. They only play the sound.