Scheduling local notification from within a Today extension
Asked Answered
W

3

12

I'm making an app that contains a Today Extension. The today extension displays a list of timers, and if the user selects one of the timers, I'd like to create and schedule a local notification for that timer.

My problem is that scheduling of notifications is done with this line of code:

UIApplication.sharedApplication().scheduleLocalNotification(notification)

which very unfortunately relies on UIApplication.sharedApplication() that is not accessible from an extension.

So my question is: How can I schedule a local notification from within a Today extension?

Funnily enough, I can make a framework with the code shared between my app and my extension and in that framework I can call:

func schedule() {
    // ...
    let settings = UIUserNotificationSettings(forTypes: .Sound | .Alert, categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    // ...
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
    // ...
}

If I then import that framework from my extension and call schedule() I get this output:

Failed to inherit CoreMedia permissions from 13636: (null)

Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7feaf3657a00>{fire date = Saturday, November 1, 2014 at 1:01:48 PM Western European Standard Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, November 1, 2014 at 1:01:48 PM Western European Standard Time, user info = (null)} with an alert but haven't received permission from the user to display alerts

Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7feaf3657a00>{fire date = Saturday, November 1, 2014 at 1:01:48 PM Western European Standard Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, November 1, 2014 at 1:01:48 PM Western European Standard Time, user info = (null)} with a sound but haven't received permission from the user to play sounds

So a module running in an extension has access to UIApplication.sharedApplication() since this code actually tries to schedule a notification, but the system is not prepared for an extension asking for permissions to display alerts and so it fails (that's what it seems anyway).

How can I solve this?

Also, I know I can just launch my app using an url and schedule the notification from the app as normal, but that is not user firendly. The whole purpose of having the today extension is so that the user doesn't have to open the app to schedule the notification. The interaction has to be quick, simple and transparent and yanking the user out of the app they're in just to run a few lines of code and then be done with it is not how I want to do things.

Whatever answered 1/11, 2014 at 13:9 Comment(0)
C
7

If anyone comes by this question, this is now achievable in iOS 10 and it's new UserNotifications framework. To schedule local notification from your app extension you can just call this line of code from within your extension:

UNUserNotificationCenter.current().add(<#UNNotificationRequest#>)
Caslon answered 20/10, 2017 at 13:34 Comment(1)
I'm not able to send it... nothing happens. notificationCenter.add(request) returns no error bu no notification is shown... any suggestion? btw I'm calling it from NetworkExtensionOverweigh
C
5

I think this may become possible if you do this:

  1. Whenever you want to schedule a local notification, make your today extension create the notification and write it's information to a file in a shared container.

  2. (You need a server) Contact your server form your today extension, ask your server to send a slient push notification (content-available: 1), which will then wake your main app up.

  3. When your app is woken up by the remote notification, reads the file created from step 1 from the shared container, schedules the local notification then goes back to sleep.

Caller answered 25/12, 2014 at 9:58 Comment(1)
There is nothing local about this approach. At this point it's almost indistinguishable from a remote notification.Karnes
D
3

This is extremely unlikely to work. App extensions aren't allowed to access sharedApplication. The fact that it's possible to prevent the compiler from noticing that you're doing so doesn't change that. If you try this in a debugger, I'd guess that sharedApplication is actually returning nil and that this is why the code is failing.

If you can find some way to make it work, don't rely on it. Apple would almost certainly regard that as a bug and fix it in a future release, breaking your extension without warning.

Dorinda answered 4/11, 2014 at 17:56 Comment(2)
@orenk86 ok thanks, maybe do you have any idea how scheduling local notification from extension?Airlike
@RomanBarzyczak You're not listening. Apple does not support this. If you think they should support it, file an enhancement request with it. It is not currently possible.Dorinda

© 2022 - 2024 — McMap. All rights reserved.