首页 文章

如何设置从存储在数组中的日期触发的本地通知?

提问于
浏览
0

我做了一个提醒应用程序,到目前为止,用户是苹果成功提交和存储数据,然后存储在一个数组中,该数组在关闭应用程序时写入文件,然后从文件加载数据当应用程序重新打开时 .

我遇到了问题,因为我想设置通知以提醒用户在特定时间完成任务,我无法回想起存储在我的阵列中的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
    }
}

我也收到警报正文和 Headers 的错误,因为我不知道如何将字符串链接在一起,然后是数组中存储的信息 .

顺便说一下,这是我从头开始学习语言以来的第一个快速项目,所以如果我是个白痴,那就容易对我说话 . :(

1 回答

  • 2

    改变这些行

    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)
    

相关问题