Reputation: 1
Im making a remind app and the so far the user is apple to successfully submit and store data from a form which is then stored in an array, this array writes to a file when the app is closed and then the data is loaded from the file when the app is reopened.
I am having an issue because I'm wanting to set the notifications to remind the user to complete the task at a certain time and I am not able to recall the NSDate that is stored in my array. Also I want to make the app work so that the notification is repeated everyday.
Here is my 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)
}
}
Here is my 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()
}
Here is my data model:
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
}
}
I am also getting an error for the alert body and title since I don't know how I chain together the string followed by the stored information in the array.
Btw this is my first swift project since learning the language from scratch so go easy on me if I'm being an idiot. :(
Upvotes: 0
Views: 866
Reputation: 207
Change these lines
localNotificationtime1.alertTitle = "Take", medicines.name
localNotificationtime1.alertBody = "It is time to take", medicines.name
to these
localNotificationtime1.alertTitle = "Take \(medicines.name)"
localNotificationtime1.alertBody = "It is time to take \(medicines.name)"
To add the time, add this code
localNotificationtime1.fireDate = NSDate(time1)
Upvotes: 2