I need a way to find what time it will be after the "unknown" amount of time for the ore to be done.
我需要一种方法来确定在完成矿石的“未知”时间之后的时间。
After you start the timer it needs to calculate when to send the local notification. I need a way so the app can be completely closed and still notify them.
启动计时器后,需要计算何时发送本地通知。我需要一种方法,以便应用程序可以完全关闭并仍然通知他们。
Thanks
谢谢
This is the error I get when I implement the "notificationsAllowed" code.
这是我实现“notificationsAllowed”代码时遇到的错误。
1 个解决方案
#1
1
To calculate the endtime you can use
计算您可以使用的结束时间
timerEndTime = NSDate(timeIntervalSinceNow:NSTimeInterval(oreWanted * 11))
So you get a NSDate
object. Now you can fire a UILocalNotification
at that calculated time:
所以你得到一个NSDate对象。现在,您可以在该计算时间触发UILocalNotification:
Note: You need to ask for notification permissions when your app starts up. I have simplified this here with notificationsAllowed
and soundsAllowed
which are boolean variables in my AppDelegate.
注意:您需要在应用启动时询问通知权限。我在这里通过notificationsAllowed和soundsAllowed简化了这一点,这是我的AppDelegate中的布尔变量。
let appDelegate = UIApplication.sharedApplication.delegate as! AppDelegate
if appDelegate.notificationsAllowed {
let notification = UILocalNotification()
notification.fireDate = timerEndTime
notification.timeZone = NSTimeZone.defaultTimeZone()
notification.alertBody = "Notification body text"
notification.alertTitle = "Notification alert title"
if appDelegate.soundsAllowed {
notification.soundName = UILocalNotificationDefaultSoundName
}
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
EDIT
编辑
Register the app for notifications in AppDelegate:
在AppDelegate中注册应用程序以获取通知:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var notificationsAllowed = false
var notificationSoundAllowed = false
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let notificationSettings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
return true
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types & UIUserNotificationType.Alert != nil {
self.notificationsAllowed = true
}
if notificationSettings.types & UIUserNotificationType.Sound != nil {
self.notificationSoundAllowed = true
}
}
#1
1
To calculate the endtime you can use
计算您可以使用的结束时间
timerEndTime = NSDate(timeIntervalSinceNow:NSTimeInterval(oreWanted * 11))
So you get a NSDate
object. Now you can fire a UILocalNotification
at that calculated time:
所以你得到一个NSDate对象。现在,您可以在该计算时间触发UILocalNotification:
Note: You need to ask for notification permissions when your app starts up. I have simplified this here with notificationsAllowed
and soundsAllowed
which are boolean variables in my AppDelegate.
注意:您需要在应用启动时询问通知权限。我在这里通过notificationsAllowed和soundsAllowed简化了这一点,这是我的AppDelegate中的布尔变量。
let appDelegate = UIApplication.sharedApplication.delegate as! AppDelegate
if appDelegate.notificationsAllowed {
let notification = UILocalNotification()
notification.fireDate = timerEndTime
notification.timeZone = NSTimeZone.defaultTimeZone()
notification.alertBody = "Notification body text"
notification.alertTitle = "Notification alert title"
if appDelegate.soundsAllowed {
notification.soundName = UILocalNotificationDefaultSoundName
}
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
EDIT
编辑
Register the app for notifications in AppDelegate:
在AppDelegate中注册应用程序以获取通知:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var notificationsAllowed = false
var notificationSoundAllowed = false
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let notificationSettings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
return true
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types & UIUserNotificationType.Alert != nil {
self.notificationsAllowed = true
}
if notificationSettings.types & UIUserNotificationType.Sound != nil {
self.notificationSoundAllowed = true
}
}