Ale*_*lex 11 notifications cocoa-touch ios uilocalnotification ios8-today-widget
我正在创建一个包含Today Extension的应用程序.今天的扩展显示计时器列表,如果用户选择其中一个计时器,我想为该计时器创建和安排本地通知.
我的问题是通过这行代码完成通知的安排:
UIApplication.sharedApplication().scheduleLocalNotification(notification)
Run Code Online (Sandbox Code Playgroud)
非常遗憾地依赖于UIApplication.sharedApplication()扩展无法访问的内容.
所以我的问题是:我如何在Today扩展中安排本地通知?
有趣的是,我可以使用我的应用程序和我的扩展程序之间共享的代码创建一个框架,在该框架中我可以调用:
func schedule() {
// ...
let settings = UIUserNotificationSettings(forTypes: .Sound | .Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
// ...
UIApplication.sharedApplication().scheduleLocalNotification(notification)
// ...
}
Run Code Online (Sandbox Code Playgroud)
如果我然后从我的扩展中导入该框架并调用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
Run Code Online (Sandbox Code Playgroud)
因此,在扩展程序中运行的模块可以访问,UIApplication.sharedApplication()因为此代码实际上尝试安排通知,但是系统没有为扩展请求显示警报的权限做好准备,因此它失败(无论如何它都是这样).
我怎么解决这个问题?
此外,我知道我可以使用网址启动我的应用程序并正常安排来自应用程序的通知,但这不是用户友好的.拥有今天扩展的整个目的是使用户不必打开应用程序来安排通知.交互必须快速,简单和透明,并且将用户从应用程序中移除,只是为了运行几行代码然后完成它不是我想要做的事情.
如果有人提出这个问题,现在可以在iOS 10中实现,它是新的UserNotifications框架.要从您的应用扩展程序安排本地通知,您只需在扩展程序中调用此行代码:
UNUserNotificationCenter.current().add(<#UNNotificationRequest#>)
Run Code Online (Sandbox Code Playgroud)
我认为如果你这样做可能会成为可能:
每当您想要安排本地通知时,请让今天的扩展程序创建通知并将其信息写入共享容器中的文件.
(您需要一台服务器)从今天的分机上联系您的服务器,要求您的服务器发送一个简单的推送通知(内容可用:1),然后唤醒您的主应用程序.
当您的应用程序被远程通知唤醒时,从共享容器中读取从步骤1创建的文件,调度本地通知然后返回休眠状态.
这是极不可能起作用的。不允许应用程序扩展访问sharedApplication. 事实上,可以阻止编译器注意到您正在这样做,但这并不会改变这一点。如果您在调试器中尝试此操作,我猜想sharedApplication实际上会返回 nil,这就是代码失败的原因。
如果您能找到某种方法使其发挥作用,请不要依赖它。苹果几乎肯定会认为这是一个错误,并在未来的版本中修复它,在没有警告的情况下破坏你的扩展。
| 归档时间: |
|
| 查看次数: |
3311 次 |
| 最近记录: |