我正在创建一个包含Today Extension的应用程序。今天的扩展显示了一个计时器列表,如果用户选择其中一个计时器,我想为该计时器创建和安排本地通知。
我的问题是通过这行代码完成通知的安排:
UIApplication.sharedApplication().scheduleLocalNotification(notification)
非常不幸地依赖 UIApplication.sharedApplication()
无法从扩展程序访问。
所以我的问题是: 如何在Today扩展中安排本地通知?
有趣的是,我可以使用我的应用程序和我的扩展程序之间共享的代码构建一个框架,在该框架中我可以调用:
func schedule() {
// ...
let settings = UIUserNotificationSettings(forTypes: .Sound | .Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
// ...
UIApplication.sharedApplication().scheduleLocalNotification(notification)
// ...
}
如果我然后从我的扩展中导入该框架并调用 schedule()
我得到这个输出:
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
因此,在扩展中运行的模块可以访问 UIApplication.sharedApplication()
因为这段代码实际上是试图安排通知,但是系统没有为扩展请求显示警报的权限做好准备,所以它失败了(无论如何它都是这样)。
我怎么解决这个问题?
此外,我知道我可以使用网址启动我的应用,并正常安排来自应用的通知,但就是这样 不 用户好友。拥有今天扩展的整个目的是使用户不必打开应用程序来安排通知。交互必须快速,简单和透明,并且将用户从应用程序中移除,只是为了运行几行代码然后完成它不是我想要做的事情。