问题
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.
回答1:
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#>)
回答2:
I think this may become possible if you do this:
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.
(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.
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.
回答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.
来源:https://stackoverflow.com/questions/26689629/scheduling-local-notification-from-within-a-today-extension