【问题标题】:Send Local Notifications while App is running in the background Swift 2.0应用程序在后台运行时发送本地通知 Swift 2.0
【发布时间】:2016-01-26 07:25:59
【问题描述】:

当用户最小化应用程序时(通过单击 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
}

MapViewController.swift

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

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

【问题讨论】:

  • 这个增量是什么?我猜你的函数没有在后台调用,因此你的通知会被安排
  • @Muneeba delta 在“someFunction”内我只是没有包含整个代码(试图保留相关部分)。我已经编辑了代码,使其更具可读性。 'if' 语句中的内容正在执行,我已经使用 'print()' 语句进行了验证。
  • 在pushNotification函数中写入print语句,看是否在bg中被调用。
  • @Muneeba 好的,当我在模拟器中最小化我的应用程序时,pushNotification() 被调用并工作,但是当我将应用程序上传到手机并最小化应用程序时,我没有收到警报就像我在模拟器中一样。
  • 你得到权限弹窗了吗?

标签: ios swift nstimer uilocalnotification uialertaction


【解决方案1】:

默认情况下,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
    }

【讨论】:

  • 是的!那行得通!谢谢!触发通知时声音不播放的原因是什么?我将通知类型设置为 [.Sound, .Alert, .Badge] 所以应该播放声音。除非我也必须在其他地方进行设置。
  • 没关系。我通过在我的 pushNotification() 函数中添加 notification.soundName = UILocalNotificationDefaultSoundName 使其工作。感谢您的帮助!
  • 您是否将此添加到您的通知中? notification.soundName = UILocalNotificationDefaultSoundName
  • 是的,我做到了,通知声音现在可以正常工作了。谢谢。
  • @Nadia 经过无数小时的搜索和教程,“beingbackgroundtaskwithname”奏效了。谢谢你的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-11
  • 2021-08-29
  • 1970-01-01
相关资源
最近更新 更多