【问题标题】:Local Notification using ios using swift [closed]使用 ios 使用 swift 进行本地通知 [关闭]
【发布时间】:2016-05-19 13:14:16
【问题描述】:

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

【问题讨论】:

标签: ios swift localnotification


【解决方案1】:

我在这里分享示例,

注册本地通知,

@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之后触发本地通知。

希望这会有所帮助:)

【讨论】:

【解决方案2】:

网上有很多教程,你可以谷歌一下。

这里有一个教程:http://jamesonquave.com/blog/local-notifications-in-ios-8-with-swift-part-1/

这里还有一个本地通知的示例:

let notification = UILocalNotification()
notification.fireDate = date
notification.alertBody = "Alert!"
notification.alertAction = "open"
notification.hasAction = true
notification.userInfo = ["UUID": "reminderID" ]
UIApplication.sharedApplication().scheduleLocalNotification(notification)

【讨论】:

  • 'UILocalNotification' 在 iOS 10.0 中已弃用:使用 UserNotifications Framework 的 UNNotificationRequest
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 2015-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多