Closed. This question needs to be more focused。它当前不接受答案。












想要改善这个问题吗?更新问题,使它仅关注editing this post的一个问题。

4年前关闭。



Improve this question




我是新手,我不知道如何实现本地通知。我已经尝试了一些代码,但是它并不完全有效,因此任何人都可以使用iOS帮助实现swift中的本地通知吗?

最佳答案

我在这里分享例子

注册本地通知,

@IBAction func registerLocal(sender: AnyObject) {
    let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
}

安排本地通知,
@IBAction func scheduleLocal(sender: AnyObject) {
    let notification = UILocalNotification()
    notification.fireDate = NSDate(timeIntervalSinceNow: 5)
    notification.alertBody = "Hey you! Yeah you! Swipe to unlock!"
    notification.alertAction = "be awesome!"
    notification.soundName = UILocalNotificationDefaultSoundName
    notification.userInfo = ["CustomField1": "w00t"]
    UIApplication.sharedApplication().scheduleLocalNotification(notification)


    guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() else { return }

    if settings.types == .None {
        let ac = UIAlertController(title: "Can't schedule", message: "Either we don't have permission to schedule notifications, or we haven't asked yet.", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
        return
    }

}

它将在5 seconds之后触发本地通知。

希望这会有所帮助:)

关于ios - 使用iOS使用Swift进行本地通知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37324331/

10-13 03:59