swift - 未收到来自 LocalNotification.fireDate 的通知

标签 swift localnotification

为什么按下按钮后不显示通知?

如果 fireDate 是 NSDate(timeIntervalSinceNow: 10) 它会工作正常

如何从数组中获取通知?

    @IBAction func buttonPressed(sender: AnyObject) {

    var stringDate = dateFormatter.stringFromDate(timePicker.date)
    alarmArray.append(stringDate)

    let fixedAlarmArray = alarmArray
    NSUserDefaults.standardUserDefaults().setObject(fixedAlarmArray, forKey: "alarmArray")
    NSUserDefaults.standardUserDefaults().synchronize()


    //Local Notification
    var localNotification:UILocalNotification = UILocalNotification()
    localNotification.alertAction = "Go to app"
    localNotification.alertBody = "Its Time!"
    localNotification.fireDate = timePicker.date
    localNotification.timeZone = NSTimeZone.defaultTimeZone()
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

    self.view.endEditing(true)

}

最佳答案

The date specified in fireDate is interpreted according to the value of this property. If you specify nil (the default), the fire date is interpreted as an absolute GMT time, which is suitable for cases such as countdown timers. If you assign a valid NSTimeZone object to this property, the fire date is interpreted as a wall-clock time that is automatically adjusted when there are changes in time zones; an example suitable for this case is an an alarm clock.

localNotification.fireDate = timePicker.date
localNotification.timeZone = NSTimeZone.defaultTimeZone()
// the problem is here, you are setting the timezone after inputing your date

你应该使用 localtimeZone 并在设置 fireDate 之前设置它

localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.fireDate = timePicker.date

注意:

In iOS 8 and later, apps that use either local or remote notifications must register the types of notifications they intend to deliver. The system then gives the user the ability to limit the types of notifications your app displays. The system does not badge icons, display alert messages, or play alert sounds if any of these notification types are not enabled for your app, even if they are specified in the notification payload.

这样做:

UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil))

创建一个扩展,将选择的时间与 0 秒结合起来,如下所示:

extension NSDate {

    var day:            Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitDay,           fromDate: self).day           }
    var month:          Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMonth,         fromDate: self).month         }
    var year:           Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear,          fromDate: self).year          }
    var minute:         Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMinute,        fromDate: self).minute }
    var hour:           Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitHour,          fromDate: self).hour   }

    var fireDate: NSDate {
        return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: hour, minute: minute, second: 0, nanosecond: 0)!
    }
}

然后按如下方式使用它:

localNotification.fireDate = timePicker.date.fireDate

关于swift - 未收到来自 LocalNotification.fireDate 的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27407245/

相关文章:

ios - 将 UILabel 添加到图像

arrays - 在字符串数组中搜索多个单词(或字符)(swift)

ios - 快速本地通知警报声

iphone - UILocalNotification 每分钟重复一次

iphone - 如何在工作日的选择列表中设置重复的 UILocal 通知

ios - 使用 Swift 每周重复本地通知

ios - 大于 Int64 的整数

swift - 如何处理/处置 TableCellViews 中旧的 NSTrackingAreas?

swift - Xcode 9 中指示器没有停止。有解决办法吗?

dart - Flutter:如何向本地通知添加按钮