1

So I can create a local notification like this

    var localNotification = UILocalNotification()
    localNotification.fireDate = NSDate(timeIntervalSinceNow: 7)
    localNotification.alertBody = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .MediumStyle, timeStyle: .ShortStyle)
    localNotification.timeZone = NSTimeZone.defaultTimeZone()
    localNotification.repeatInterval = NSCalendarUnit.CalendarUnitMinute
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

This creates a notification with the date right now and then starts firing it off every minute for eternity. If the time the notification was created is 5pm it will say 5pm every time it fires, even if 30 minutes later the time is 5:30.

What do I need to do to make the alert body dynamic?

I read in the docs it expects a string for alertBody so I am having trouble trying to figure out how to set the message body with a function call or a time stamp or a value that changes.

https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/#//apple_ref/occ/instp/UILocalNotification/alertBody

Basically I want my app to calculate a value at the time the notification is about to fire, not at the time it is set, to create the message. The example about uses a notification every minute for symplicty so you can see this code in action. In the real app I want to fire a daily notification at a pre determined time. EG Once a day at 7am and calculate a value at that time.

humanbeing
  • 1,617
  • 3
  • 17
  • 30
  • possible duplicate of [Is there a simple way to edit / modify a UILocalNotification](http://stackoverflow.com/questions/3842252/is-there-a-simple-way-to-edit-modify-a-uilocalnotification) – Chris Slowik Sep 29 '15 at 03:55
  • 1
    @ChrisSlowik I guess this question becomes: Is there a way to dynamically create notifications once a day, like a cron job? – humanbeing Sep 29 '15 at 04:06
  • But your code does not fire the notification. The system does. And at that time your app might not even be running, so it cannot possibly do a calculation then. Do you see? – matt Sep 29 '15 at 04:14
  • Your app would need to create a notification. in the code for handling the notification, you could create a new one for 24 hr i suppose. – Chris Slowik Sep 29 '15 at 04:18
  • that is an interesting idea @ChrisSlowik, do you have a snippet of any notification handlers? It doesn't have to be specific to this use case, just something that does anything when a notification fires – humanbeing Sep 29 '15 at 04:26
  • @humanbeing Your app is not informed when a local notification fires, unless it happens to be running in the foreground at that moment. Hence my answer (which is right). – matt Sep 29 '15 at 16:35

1 Answers1

1

You can't make it dynamic. You would need separate notifications with separate text for each of the separate times.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • So I made a simple example for explanation's sake, but I can see that is biting me here. I want to do a daily notification with a calculated value. Is there a way to make a cron job or something that makes a notification using a dynamic value? – humanbeing Sep 29 '15 at 04:03
  • I think you've misapprehended what a local notification is. It is a message to be delivered by the system to the user, and only if the user permits. And how can there be a cron job when your code doesn't even run unless it is frontmost with the screen unlocked? – matt Sep 29 '15 at 04:12
  • 1
    I think you have misapprehended what I am asking. I am new to IOS and swift and am not saying I know the best way to do this. I am looking for a way to present any kind of dynamic information for a user. Does swift have any way of doing this? Apps like Moves, Darksky etc send info that is not a static string. – humanbeing Sep 29 '15 at 04:14
  • So far you have not even said what "this" really is. But I suspect that you have an idea that simply cannot be realized in the sandboxed single-app world of iOS. – matt Sep 29 '15 at 04:16
  • Maybe you should look into remote notifications. – matt Sep 29 '15 at 04:17
  • A lot of the stuff mentioned is Local Notifications - alerts from dark sky are not push notifications, they're created by the app in response to updated weather information. – Chris Slowik Sep 29 '15 at 04:18
  • But you have to think about how the weather information gets updated. The app can't do it if it isn't running. – matt Sep 29 '15 at 04:19
  • 1
    @matt Thank you for finally giving an answer that might point me towards a solution. – humanbeing Sep 29 '15 at 04:20
  • @matt I don't know how those get updated, that is why I am asking questions about how things work on a webforum. Moves tracks data locally, its not obvious it would need remote notifications – humanbeing Sep 29 '15 at 04:21
  • Also think about Background App Refresh. – matt Sep 29 '15 at 04:22
  • You are changing the subject. Moves is totally different. Motion detection and location tracking are built into the device. – matt Sep 29 '15 at 04:24
  • @matt it isnt changing the subject. moves give you a notification in the morning that tells you how much activity it did the previous day. I am talking about the notification in moves, not the tracking. Reread my post and it is clear what I said – humanbeing Sep 29 '15 at 20:09
  • However, the point I'm making remains. The only way Moves can do that is to create the notification _while it is running_. It might be running in the background doing Core Location or Core Motion the previous day, but it would still have to be _running_ and create the notification including the text. The text cannot magically modify itself the next morning, which is what you appear to be asking for. And the app cannot magically launch itself in order to create the notification. – matt Sep 29 '15 at 21:00
  • @matt lol. It is clear if you never used moves or darksky. I said those apps send notifications that are not static. Doesnt matter what the app does, the notifications are not static. Was stated plainly. – humanbeing Sep 30 '15 at 01:37
  • @matt your point is correct. you answered my question after much back and forth. Thank you for answering it and letting me know where to investigate. – humanbeing Sep 30 '15 at 01:38