ios - UILocalNotification 不发送

标签 ios iphone swift cocoa-touch uilocalnotification

我正在尝试发送如下所示的 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
    }
}

关于ios - UILocalNotification 不发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38392946/

相关文章:

iphone - XCode 4拍摄通用应用(iPhone/iPad)的屏幕截图

ios - 我如何通过单击单选按钮获取 TextView 的 ID

ios - UIAlertView 警报在长按手势识别器中重复三次

iphone - UITableview dequeueReusableCellWithIdentifier 重复行

iphone - 剪切绘图后如何将上下文重置为原始矩形?

ios - 将 var 转换为 float 以进行数学运算

swift - Firebase 管理下载 API 不可用

ios - 如何在选择和取消选择单元格时仅保留数组中的不同元素?

ios - 在 Apple 的 App Store 发布 Phonegap Build App - 体验?

ios - NSURLConnection 不使用默认凭据