簡體   English   中英

如何設置本地通知從存儲在數組中的日期開始觸發?

[英]How to set a local notification to fire from a date stored in an array?

我正在制作一個提醒應用程序,到目前為止,用戶是蘋果公司,可以成功地提交和存儲來自表單的數據,然后將其存儲在數組中。當應用程序關閉時,該數組將寫入文件,然后從文件中加載數據重新打開應用程序時。

我遇到了一個問題,因為我想設置通知以提醒用戶在特定時間完成任務,而我無法調用存儲在數組中的NSDate。 我也想使該應用程序正常工作,以便每天重復進行通知。

這是我的TableViewController:

class MedicineTableViewController: UITableViewController {



//MARK Properties

var medicines = [Medicine]()

override func viewDidLoad() {
    super.viewDidLoad()

    //Notifications Setup
    let reminderActionOkay = UIMutableUserNotificationAction()
    reminderActionOkay.identifier = "Okay"
    reminderActionOkay.title = "Okay"
    reminderActionOkay.activationMode = UIUserNotificationActivationMode.Background
    reminderActionOkay.destructive = true
    reminderActionOkay.authenticationRequired = false

    let reminderActionOpen = UIMutableUserNotificationAction()
    reminderActionOpen.identifier = "Open App"
    reminderActionOpen.title = "Open App"
    reminderActionOpen.activationMode = UIUserNotificationActivationMode.Background
    reminderActionOpen.destructive = false
    reminderActionOpen.authenticationRequired = true

    //Put the different types of notifications into a category
    let ReminderCategory = UIMutableUserNotificationCategory()
    ReminderCategory.identifier = "reminderCategory"
    ReminderCategory.setActions([reminderActionOpen, reminderActionOkay], forContext: UIUserNotificationActionContext.Default)
    ReminderCategory.setActions([reminderActionOpen, reminderActionOkay], forContext: UIUserNotificationActionContext.Minimal)


//MARK: Notification Schedule
func scheduleLocalNotification() {
    let localNotificationtime1 = UILocalNotification()
    localNotificationtime1.alertTitle = "Take", medicines.name
    localNotificationtime1.alertBody = "It is time to take", medicines.name
    localNotificationtime1.alertAction = "Show Details"
    localNotificationtime1.fireDate = medicine.time1 // 1 Day repeating
    localNotificationtime1.timeZone = NSTimeZone.defaultTimeZone()
    localNotificationtime1.soundName = UILocalNotificationDefaultSoundName
    localNotificationtime1.applicationIconBadgeNumber = 1
    localNotificationtime1.category = "reminderCategory"
    UIApplication.sharedApplication().scheduleLocalNotification(localNotificationtime1)

    let localNotificationtime2 = UILocalNotification()
    localNotificationtime2.alertTitle = "Take", medicines.name
    localNotificationtime2.alertBody = "It is time to take", medicines.name
    localNotificationtime2.alertAction = "Show Details"
    localNotificationtime2.fireDate = medicines.time2
    localNotificationtime2.timeZone = NSTimeZone.defaultTimeZone()
    localNotificationtime2.soundName = UILocalNotificationDefaultSoundName
    localNotificationtime2.applicationIconBadgeNumber = 1
    localNotificationtime2.category = "reminderCategory"
    UIApplication.sharedApplication().scheduleLocalNotification(localNotificationtime2)

    let localNotificationtime3 = UILocalNotification()
    localNotificationtime3.alertTitle = "Take",  medicines.name
    localNotificationtime3.alertBody = "It is time to take", medicines.name
    localNotificationtime3.alertAction = "Show Details"
    localNotificationtime3.fireDate = medicines.time3
    localNotificationtime3.timeZone = NSTimeZone.defaultTimeZone()
    localNotificationtime3.soundName = UILocalNotificationDefaultSoundName
    localNotificationtime3.applicationIconBadgeNumber = 1
    localNotificationtime3.category = "reminderCategory"
    UIApplication.sharedApplication().scheduleLocalNotification(localNotificationtime3)

    let localNotificationtime4 = UILocalNotification()
    localNotificationtime4.alertTitle = "Take", medicines.name
    localNotificationtime4.alertBody = "It is time to take", medicines.name
    localNotificationtime4.alertAction = "Show Details"
    localNotificationtime4.fireDate = medicines.time4
    localNotificationtime4.timeZone = NSTimeZone.defaultTimeZone()
    localNotificationtime4.soundName = UILocalNotificationDefaultSoundName
    localNotificationtime4.applicationIconBadgeNumber = 1
    localNotificationtime4.category = "reminderCategory"
    UIApplication.sharedApplication().scheduleLocalNotification(localNotificationtime4)

    let localNotificationtime5 = UILocalNotification()
    localNotificationtime5.alertTitle = "Take", medicines.name
    localNotificationtime5.alertBody = "It is time to take", medicines.name
    localNotificationtime5.alertAction = "Show Details"
    localNotificationtime5.fireDate = medicines.time5
    localNotificationtime5.timeZone = NSTimeZone.defaultTimeZone()
    localNotificationtime5.soundName = UILocalNotificationDefaultSoundName
    localNotificationtime5.applicationIconBadgeNumber = 1
    localNotificationtime5.category = "reminderCategory"
    UIApplication.sharedApplication().scheduleLocalNotification(localNotificationtime5)


}

}

這是我的AppDelegate:

import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    // Override point for customization after application launch.
    return true
}
  func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    // Point for handling the local notification when the app is open.
    // Showing reminder details in an alertview
    UIAlertView(title: notification.alertTitle, message: notification.alertBody, delegate: nil, cancelButtonTitle: "OK").show()
}

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
    // Point for handling the local notification Action. Provided alongside creating the notification.
    if identifier == "ShowDetails" {
        // Showing reminder details in an alertview
        UIAlertView(title: notification.alertTitle, message: notification.alertBody, delegate: nil, cancelButtonTitle: "OK").show()
    } else if identifier == "Okay" {

    } else if identifier == "Open App" {
        //Launches app when open app button pressed
        UIApplicationState.Active
    }
    completionHandler()
}

這是我的數據模型:

import UIKit

class Medicine : NSObject, NSCoding {


var name: String
var time1: NSDate
var time2: NSDate
var time3: NSDate
var time4: NSDate
var time5: NSDate


init?(name: String, time1: NSDate, time2: NSDate, time3: NSDate, time4: NSDate, time5: NSDate) {

    self.name = name
    self.time1 = time1
    self.time2 = time2
    self.time3 = time3
    self.time4 = time4
    self.time5 = time5

    super.init()


    if name.isEmpty {
        return nil
    }
}

由於不知道如何將字符串和數組中存儲的信息鏈接在一起,因此警報主體和標題也出現錯誤。

順便說一句,這是自從零開始學習語言以來我的第一個快速項目,所以如果我是個白痴,請對我輕松一點。 :(

更改這些行

localNotificationtime1.alertTitle = "Take", medicines.name
localNotificationtime1.alertBody = "It is time to take", medicines.name

對這些

localNotificationtime1.alertTitle = "Take \(medicines.name)"
localNotificationtime1.alertBody = "It is time to take \(medicines.name)"

要添加時間,請添加以下代碼

localNotificationtime1.fireDate = NSDate(time1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM