从 Today 扩展中安排本地通知 [英] Scheduling local notification from within a Today extension

查看:19
本文介绍了从 Today 扩展中安排本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个包含 Today Extension 的应用.今天的扩展程序显示一个计时器列表,如果用户选择其中一个计时器,我想为该计时器创建和安排本地通知.

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)

很遗憾,它依赖于无法从扩展程序访问的 UIApplication.sharedApplication().

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

所以我的问题是:如何从 Today 扩展程序中安排本地通知?

有趣的是,我可以使用我的应用程序和扩展程序之间共享的代码创建一个框架,并且在该框架中我可以调用:

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)
    // ...
}

如果我然后从我的扩展中导入该框架并调用 schedule() 我得到这个输出:

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

因此,在扩展中运行的模块可以访问 UIApplication.sharedApplication(),因为此代码实际上尝试安排通知,但系统并未为请求显示警报权限的扩展做好准备所以它失败了(反正看起来就是这样).

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?

另外,我知道我可以使用 url 启动我的应用程序并正常安排来自应用程序的通知,但这不是用户友好.拥有今天扩展的全部目的是让用户不必打开应用程序来安排通知.交互必须快速、简单和透明,并且将用户从他们所在的应用程序中拉出来,只需要运行几行代码,然后就可以完成,这不是我想做的事情.

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.

推荐答案

如果有人问过这个问题,现在可以在 iOS 10 和它的新 UserNotifications 框架中实现.要从您的应用扩展程序安排本地通知,您只需在您的扩展程序中调用以下代码行:

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#>)

这篇关于从 Today 扩展中安排本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆