简体   繁体   English

UILocalNotification不发送

[英]UILocalNotification doesn't send

I'm trying to send a UILocalNotification like this: 我试图发送这样的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)
    }

I tried to put a break point on this method and it is being called and run, but the notification doesn't sent at all. 我试图在此方法上设置一个断点,并且正在调用它并运行它,但是通知根本没有发送。

Anyone has an idea why? 有人知道为什么吗?

You gotta to register the Notification first 您必须先注册通知

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

Here's my demo for Notification firing at a specific time 这是我在特定时间触发通知的演示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM