如何在Swift中使用UILocalNotification

时间:2014-07-01 22:11:03

标签: ios swift uilocalnotification

我试图弄清楚如何在swift中设置UILocalNotification,但我没有太多运气。我正在尝试这个:

var notification = UILocalNotification()
notification.timeZone = NSTimeZone.defaultTimeZone()
var dateTime = NSDate.date()
notification.fireDate(dateTime)
notification.alertBody("Test")
UIApplication.sharedApplication().scheduleLocalNotification(notification)

首先,我不确定这是否是获取当前日期时间的正确方法。在.Net中,我只会做DateTime.Now()。

其次,当我尝试这个时,我得到一个错误,上面写着:

  

'(@ lvalue NSDate!) - > $ T3'与'NSDate'

不同

不幸的是我不知道这意味着什么或如何继续。

6 个答案:

答案 0 :(得分:10)

首先,使用初始化语法构造NSDate

let dateTime = NSDate()

The documentation显示了ObjC便捷构造函数如何映射到Swift初始化器。如果文档为某个类显示init(),则可以使用该类的名称来调用它:对于NSDateinit()表示您调用NSDate()init(timeInterval:sinceDate:)表示你致电NSDate(timeInterval: x, sinceDate: y)等等。

第二:fireDate不是方法,而是属性。您应该分配给它而不是试图调用它:

notification.fireDate = dateTime

同样为alertBody

您还可以通过命令单击Swift源文件中的类名(或其他API符号)来查找Cocoa API的Swift语法;这导致Xcode生成相关头文件的“Swift-ified”版本。

答案 1 :(得分:6)

func setupNotificationReminder() {
    var title:String = "Your reminder text goes here"

    let calendar = NSCalendar.currentCalendar()
    let calendarComponents = NSDateComponents()
    calendarComponents.hour = 7
    calendarComponents.second = 0
    calendarComponents.minute = 0
    calendar.timeZone = NSTimeZone.defaultTimeZone()
    var dateToFire = calendar.dateFromComponents(calendarComponents)

    // create a corresponding local notification
    let notification = UILocalNotification()

    let dict:NSDictionary = ["ID" : "your ID goes here"]
    notification.userInfo = dict as! [String : String]
    notification.alertBody = "\(title)"
    notification.alertAction = "Open"
    notification.fireDate = dateToFire
    notification.repeatInterval = .Day  // Can be used to repeat the notification
    notification.soundName = UILocalNotificationDefaultSoundName
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

答案 2 :(得分:3)

没有回答你的问题,但值得注意:

notification.fireDate(dateTime)
notification.alertBody("Test")

也会抛出编译错误,说它无法找到init。这样做

notification.fireDate = NSDate(timeIntervalSinceNow: 15)
notification.alertBody = "Notification Received"

答案 3 :(得分:2)

还支持创建日期:

NSDate(timeIntervalSinceNow: 15)

答案 4 :(得分:2)

在Swift中,使用唯一键取消特定的本地通知:

func cancelLocalNotification(UNIQUE_ID: String){

        var notifyCancel = UILocalNotification()
        var notifyArray = UIApplication.sharedApplication().scheduledLocalNotifications

        for notifyCancel in notifyArray as! [UILocalNotification]{

            let info: NSDictionary = notifyCancel.userInfo as! [String : String]

            if info[UNIQUE_ID]!.isEqual(UNIQUE_ID){

                UIApplication.sharedApplication().cancelLocalNotification(notifyCancel)
            }else{

                println("No Local Notification Found!")
            }
        }
    }

答案 5 :(得分:0)

将单独的一些组件分开是很好的:

foreach (var textbox in GetSubControlsOf<TextBox>(this))
{
       validData &= !string.IsNullOrWhiteSpace(textbox.Text);
}

现在,如果用户已注册远程通知,您可以致电private let kLocalNotificationMessage:String = "Your message goes here!" private let kLocalNotificationTimeInterval:NSTimeInterval = 5 private func LocalNotification() -> UILocalNotification { var localNotification:UILocalNotification = UILocalNotification() localNotification.fireDate = NSDate(timeIntervalSinceNow:kLocalNotificationTimeInterval) localNotification.alertBody = kLocalNotificationMessage return localNotification } private func ScheduleLocalNotificationIfPossible() { if (UIApplication.sharedApplication().isRegisteredForRemoteNotifications()) { UIApplication.sharedApplication().scheduleLocalNotification(LocalNotification()) } } 来安排本地通知。