gpt4 book ai didi

ios - 如何将本地通知设置为从存储在数组中的日期开始触发?

转载 作者:行者123 更新时间:2023-11-28 13:08:11 25 4
gpt4 key购买 nike

我正在制作一个提醒应用程序,到目前为止,用户是苹果成功提交和存储来自表单的数据,然后存储在一个数组中,当应用程序关闭时,该数组写入一个文件,然后加载数据重新打开应用程序时从文件中获取。

我遇到了一个问题,因为我想设置通知以提醒用户在特定时间完成任务,但我无法调用存储在数组中的 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)

关于ios - 如何将本地通知设置为从存储在数组中的日期开始触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32166674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com