UILocalNotification 不发送

UILocalNotification doesn't send

我正在尝试发送这样的 UILocalNotification

func sendNotifiaction() {
        let notification = UILocalNotification()
        notification.userInfo = [
            "Identifier": self.identifier!
        ]
        notification.alertTitle = "Alarm!"
        notification.alertBody = "test"
        //notification.soundName = "Temporary-bleep-sound.aiff"
        notification.category = "category"

        UIApplication.sharedApplication().scheduleLocalNotification(notification)
    }

我试图在这个方法上设置一个断点,它正在被调用并且运行,但是根本没有发送通知。

有人知道为什么吗?

你得先注册通知

UIApplication.sharedApplication().cancelAllLocalNotifications()
let settings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)

这是我在特定时间触发通知的演示

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        UIApplication.sharedApplication().cancelAllLocalNotifications()
        let settings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(settings)

        let localNotification1 = UILocalNotification()
        localNotification1.alertBody = "Your alert message at 9:00 pm"
        localNotification1.timeZone = NSTimeZone.defaultTimeZone()
        localNotification1.fireDate = self.getNinePMDate()
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification1)
        return true
    }

    func getNinePMDate() -> NSDate? {
        let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
        let now: NSDate! = NSDate()
        let date21h = calendar.dateBySettingHour(21, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)
        return date21h
    }
}

您应该在 appdelegate 中实施 didReceiveLocalNotification 并且您必须手动显示带有标题和消息的警报视图作为通知的 alertbody