Ios 当应用程序在后台Swift 2.0中运行时发送本地通知

Ios 当应用程序在后台Swift 2.0中运行时发送本地通知,ios,swift,nstimer,uilocalnotification,uialertaction,Ios,Swift,Nstimer,Uilocalnotification,Uialertaction,我试图在用户最小化应用程序时(通过单击iPhone的主页按钮或锁定手机),向用户发送“推送通知式”警报 我的应用程序持续解析XML文件(每10秒一次),我希望应用程序继续运行,以便在我的程序中满足某些条件后,即使在用户最小化应用程序或锁定手机后,它也会向用户发送本地通知 我从教程中蹦蹦跳跳,每个人似乎都在“安排”通知,但这对我不起作用,因为我的通知不是基于时间的,而是基于满足的条件 到目前为止我所做的: AppDelegate.swift func application(application

我试图在用户最小化应用程序时(通过单击iPhone的主页按钮或锁定手机),向用户发送“推送通知式”警报

我的应用程序持续解析XML文件(每10秒一次),我希望应用程序继续运行,以便在我的程序中满足某些条件后,即使在用户最小化应用程序或锁定手机后,它也会向用户发送本地通知

我从教程中蹦蹦跳跳,每个人似乎都在“安排”通知,但这对我不起作用,因为我的通知不是基于时间的,而是基于满足的条件

到目前为止我所做的:

AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))
    return true
}
// Some function
func someFunction(delta: Int) {
    if delta < 100 {
        // Send alert to user if app is open
        let alertView = UIAlertController(title: "This is an Alert!", message: "", preferredStyle: UIAlertControllerStyle.Alert)
        alertView.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alertView, animated: true, completion: nil)

        // Send user a local notification if they have the app running in the bg
        pushTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("pushNotification"), userInfo: nil, repeats: false)
    }
}

// Send user a local notification if they have the app running in the bg
func pushNotification() {
    let notification = UILocalNotification()
    notification.alertAction = "Go back to App"
    notification.alertBody = "This is a Notification!"
    notification.fireDate = NSDate(timeIntervalSinceNow: 1)
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
MapViewController.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))
    return true
}
// Some function
func someFunction(delta: Int) {
    if delta < 100 {
        // Send alert to user if app is open
        let alertView = UIAlertController(title: "This is an Alert!", message: "", preferredStyle: UIAlertControllerStyle.Alert)
        alertView.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alertView, animated: true, completion: nil)

        // Send user a local notification if they have the app running in the bg
        pushTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("pushNotification"), userInfo: nil, repeats: false)
    }
}

// Send user a local notification if they have the app running in the bg
func pushNotification() {
    let notification = UILocalNotification()
    notification.alertAction = "Go back to App"
    notification.alertBody = "This is a Notification!"
    notification.fireDate = NSDate(timeIntervalSinceNow: 1)
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
//一些函数
func someFunction(delta:Int){
如果δ<100{
//如果应用程序已打开,则向用户发送警报
让alertView=UIAlertController(标题:“这是一个警报!”,消息:,首选样式:UIAlertControllerStyle.Alert)
addAction(UIAlertAction(标题:“确定”,样式:UIAlertActionStyle.Default,处理程序:nil))
self.presentViewController(alertView,动画:true,完成:nil)
//如果应用程序在bg中运行,则向用户发送本地通知
pushTimer=NSTimer.scheduledTimerWithTimeInterval(0.5,目标:self,选择器:选择器(“pushNotification”),用户信息:nil,重复:false)
}
}
//如果应用程序在bg中运行,则向用户发送本地通知
func pushNotification(){
let notification=UILocalNotification()
notification.alertAction=“返回应用程序”
notification.alertBody=“这是一个通知!”
notification.fireDate=NSDate(timeIntervalSinceNow:1)
UIApplication.sharedApplication().scheduleLocalNotification(通知)
}

当应用程序打开时,警报效果很好,但当我最小化手机上的应用程序时,通知永远不会显示。我假设应用程序在后台时没有运行,或者我不太理解这个概念。非常感谢您的帮助。

默认情况下,NSTimer仅在模拟器中工作。尝试将其添加到AppDelegate文件:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil))

application.beginBackgroundTaskWithName("showNotification", expirationHandler: nil)

        return true
    }

这个三角洲是什么?我猜您的函数没有在后台调用,因此您的通知力无法得到scheduled@Muneebadelta在“someFunction”中,我只是没有包含整个代码(试图保留相关部分)。我对代码进行了编辑,使其更具可读性。“if”语句中的内容正在执行,我已经用“print()”语句进行了验证。在pushNotification函数中编写print语句,看看它是否在bg中被调用。@Muneeba Ok,当我在模拟器中最小化应用程序时,pushNotification()被调用并起作用,但当我将应用程序上载到手机并最小化应用程序时,我没有像在模拟器中那样得到警报。你得到权限弹出窗口了吗?是的!成功了!非常感谢。当通知被触发时,声音不播放有什么原因吗?我正在将通知类型设置为[.Sound、.Alert、.Badge],以便播放声音。除非我也要在别的地方安装,没关系。我通过在pushNotification()函数中添加notification.soundName=UILocalNotificationDefaultSoundName使其正常工作。谢谢你的帮助!您是否将此添加到通知中?notification.soundName=UILocalNotificationDefaultSoundName是的,通知声音正在工作。谢谢。@Nadia经过无数个小时的搜索和教程,“beingbackgroundtaskwithname”成功了。谢谢你的回答