在Today扩展程序中计划本地通知 [英] Scheduling local notification from within a Today extension

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

问题描述

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



我的问题是通过这行代码来安排通知:

  UIApplication.sharedApplication()。scheduleLocalNotification非常不幸地依赖于 UIApplication.sharedApplication() $ b 



<



因此,我的问题是:如何安排今天延伸中的本地通知?有趣的是,我可以使用我的应用程序和我的扩展之间共享的代码框架,在这个框架中我可以调用:

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

如果我从我的扩展中导入框架并调用 schedule code>我得到这个输出:

 无法继承CoreMedia权限13636:(null)

尝试计划本地通知< UIConcreteLocalNotification:0x7feaf3657a00> {fire date = 2014年11月1日星期六下午1:01:48西欧标准时间,时区=(null),重复间隔= 0,重复count = UILocalNotificationInfiniteRepeatCount,下一个火日期= 2014年11月1日星期六下午1:01:48西欧标准时间,用户信息=(null)}有警报,但没有收到用户的许可显示警报

尝试安排本地通知< UIConcreteLocalNotification:0x7feaf3657a00> {fire date = 2014年11月1日星期六下午1:01:48西欧标准时间,时区=(null),重复间隔= 0,重复计数= UILocalNotificationInfiniteRepeatCount,下一个火日期=星期六,2014年11月1日下午1:01:48西欧标准时间,用户信息=(null)}有声音,但没有收到用户的播放许可声音

因此,在扩展中运行的模块可以访问 UIApplication.sharedApplication ),因为这个代码实际上试图调度通知,但是系统没有为扩展请求显示警报的权限做准备,所以它失败了(这是它似乎无论如何)。



我如何解决这个问题?



此外,我知道我可以使用网址启动我的应用程序,应用程序正常,但这是用户firendly。具有今天分机的整个目的是使得用户不必打开应用来安排通知。交互必须是快速,简单和透明的,使用户离开应用程序,他们只是为了运行几行代码,然后完成它不是我想做的事情。

解决方案

我想这可能会成为可能,如果你这样做:


    p>(您需要一个服务器)从您的今天的分机联系您的服务器,请您的服务器发送一个slient推送通知(内容可用:1),然后将唤醒您的主应用程序。


  1. 当您的应用程序被远程通知唤醒时,从共享容器读取从步骤1创建的文件,安排本地通知,然后返回睡眠状态。



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.

解决方案

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.

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

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