Jump to content
Sign in to follow this  
Rollo62

Using iOS LocalNotification with EKAlarm, EKEvent, EKEventStore as "short time, timer alarm"

Recommended Posts

Hi there,

 

I'm looking for ways to create a simple short-time timer under iOS.

 

via LocalNotifications:

One possibility is to use LocalNotifications, but its not much customizeable, nor to make it prominent.
Also it seems not to be too reliable

Quote

Note
Every attempt is made to deliver local and remote notifications in a timely manner, but delivery isn't guaranteed.

The PushKit framework offers a more timely delivery mechanism for specific types of notifications, such as those used for VoIP and watchOS complications. For more information, see PushKit.

via PushKit:

that looks more interesting, but I'm afraid that all notifications (even the 15 sec. timer) has to be launched via Apples APNs server.

Quote

PushKit notifications differ from the ones you handle with the UserNotifications framework.

Instead of displaying an alert, badging your app’s icon, or playing a sound,

PushKit notifications wake up or launch your app and give it time to respond. Both PushKit and UserNotifications use the Apple Push Notification service (APNs) to deliver push notifications to user devices.

 

via Calendar system:

The Apple doc say that this is not the right way anyway, but to use the EventKit Calendar and EKEventStore.

https://developer.apple.com/documentation/eventkit

 

To access the EKEventKit would mean that the app needs calendar permissions

Quote

To access the user's calendar events, reminders, and contacts through EventKitUI, you need to include descriptions for:

    NSCalendarUsageDescription

    NSReminderUsageDescription

    NSContactsUsageDescription

Which is quite heavy requirements for a simple timer notification, of <= 2h.

But at least with that I could possibly change the alarm sound.

The alarm would then be handled from the central calendar app of the phone,
I think that this also will open my app via LocalNotification, under the right condition, when user presses the open button.

Moreover, I'm not sure if this adds an visible event in the calendar, what I don't want.

But I think there should be ways to hide/delete an invisible event, but Apple docs, see note below, restricts that.

 

There are some old calendar examples already in the D.P.F. component suite, the first 3rd party componentes of FMX, as far as I know.

I think they are a little outdated by permissions now, but maybe basically still OK.

 

The URL is deprecated, it shall use EMailAddress instead. Which also sets Sound to nil, and doesn't seem to be able to open an related app.

Via EventKit this is maybe still possible, maybe with the EventStoreChanged, with a lot of overhead (see note below).

Quote

It’s possible for another process or app to modify the Calendar database while your app is running. If your app fetches calendar events or reminders, register to be notified about changes to the Calendar database to ensure the calendar and reminder information you display to the user is current.

Quote

EventStoreChanged:

This notification is posted whenever changes are made to the Calendar database, including adding, removing, and changing events or reminders.

Individual changes are not described. When you receive this notification, you should refetch all EKEvent and EKReminder objects you have accessed,
as they are considered stale.

If you are actively editing an event and do not wish to refetch it unless it is absolutely necessary to do so, you can call the refresh method on it. If the method returns true, you do not need to refetch the event.

 

Is there any lighter middle way:

Two notes from Apple docs:

Quote

An alarm is not intended to serve as a UILocalNotification. An alarm requires you to create an event or reminder that is visible in the user’s Calendar or Reminders app. A UILocalNotification is better suited for general purposes that don’t involve the Calendar database.

 

My question is if there is maybe a middle way, or an extension to the LocalNotification, to allow a simple alarm timer running

without too much permission requests or overhead ?

 

 

 

Share this post


Link to post

What are the actual requirements for the short-time timer? Is it to display a notification? How short is the "short-time"? Any other requirements?

Share this post


Link to post

@Dave Nottage

Yes "short time" means about 30sek ... 2h max., typically around a few minutes), while it should do the following
1. at least show a notification alert (this works also in normal FMX)-

2. it should exchange the default "bling" sound with another short ringtone

   (Edit:  Finally I found my issue with SoundName, it shall not include the full absolute path, only the filename, see references below)

3. maybe recurring alarms, if the user doesn't react at first note (this should work with normal FMX to)
4. recurring alarms with a specific delay (this should work with normal FMX too, it seems only a range can be set (sec,min,h,...), not e.g. 20 sec. ?)
5. better to have a "customized" looking notification, which looks like a custom popup
6. while this notation popup stays a little longer visible than normal notification

7. best of all if it would wake and notify the app in the background, also before the user presses the button (I think this is probably not possible under iOS)
8. the notified app could possibly start a text-to-speech voice in the background, before the user clicked, explaining additional info to the user

 

I think not all from my wishlist is possible, but I try to get as far as I could.


I have checked also with your KastriFree implementation, with what I did the following:

  • create a .caf file from .mp3 via "afconvert", doing the following steps:
    copy Snd_Bell_001.mp3 to ~/Downloads
    cd ~/Downloads
    cd /Users/username/Downloads
    afconvert -f caff -d LEI16 '~/Downloads/Snd_Bell_001.mp3' '~/Downloads/Snd_Bell_001.caf'
     
  • add the .caf file to the ressources, to put them in iOS
  • on iOS I create sub-folder /Library/Sounds and copy the .caf file to there, as described here
    (
    maybe I may skip the file copy to /Sounds, and use the file directly from the bundle, but I can check the right paths later)
    Quote

    For all other sounds, the UNNotificationSound object looks only in the following locations:
    - The /Library/Sounds directory of the app's container directory.
    - The /Library/Sounds directory of one of the app's shared group container directories.
    - The main bundle of the current executable.

     

  • The sound file works in forground, what I could test when playing it with mediaplayer
  • When creating a scheduled notification, I have to use the SoundName as filename (e.g. "tone.caf") only, it shall not include the full path.
    Edit: Only then iOS is searching in all the places above, for the SoundName.

So I can confirm that your UserNotifications in KastriFree implementation work well so far.
I think an app can get until step 2./3. so far, but all above that will be not possible (at least I can see no way to do it).

Maybe there are other hidden APIs, or new idea, in iOS that could do it, but I cannot find them.
 

 

Share this post


Link to post

I found some API to customize UserNotifications UI, not looked too deep into it, but maybe that could do what I want.

At least the UI customization part.

Edited by Rollo62

Share this post


Link to post
7 hours ago, Rollo62 said:

I found some API to customize UserNotifications UI, not looked too deep into it, but maybe that could do what I want.

Oddly enough, I started looking at that recently, to include images in push notifications (which is way easier on Android). Unfortunately, it requires an iOS app extension which you have to create with Xcode (not real hard to do, mind you). I have shelved it for the time being because I have not been able to make it work as yet. I hope to come back to it in the next few days.

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×