Trigger Local Notifications Automatically Daily on Dynamic Time Given in Arrays Objective C iOS

Trigger local notifications automatically daily on dynamic time given in arrays Objective c ios

Important Note: Developer can not do anything if application is in kill state or in background for a long time. So this can not be done.

There are many questions for your requirement:

1.1: How many days data you have in one service, for a month, for a year or ?

1.2: Why not push?

1.3: Why do you want to schedule local notification on daily basis while you can schedule many at once?

1.4: There is no way to make changes in the app when its not opened (killed by user) so you can not do anything in that case.

Now come to what we can do:

2.1: Considering you are getting monthly data. So you should schedule all the local notifications at once in for loop by increasing the day component and setting the corresponding time (from your arrays by day index). And its your luck if user opens the application once in a month (90% chances he will) then you can request to server for next month data and do the same.

2.2: Why not this is being handled at server end and use push notifications instead of local. Server guys can configure a cron job for every minute which will fetch the all hours and minutes for users and will send the push.

My suggestion not to go with local but all maximum you can go with 2.1 (schedule all month notifications at once).

how to call a web service daily on particular time using local notification in objective c

you cant schedule something like this offline but you have 2 options that differ

  1. with thr app background refresh API you may come close. If you opt in to that api, ios will wakeup your app when it has spare cycles and will give you cpu time to run some code and allow you to do this.


    the background refresh api was meant for 'periodic' updates like this IMO. What you cannot do with it though, is schedule any EXACT times/dates/intervals you want to be woken. You can recommend times to ios but it may or may not stick to the plan (this depends on device use .... battery .... time of day...... etc)

  2. another option are a backend that sends 'silent' push notifications at your required time. IOS would wake your app for those notifications and as they are silent, the user wont see it.

  3. you can have a backend send you non silent pushes. Your app will be launched on tapping the notification and you can do whatever you like

==> option 1 works offline, option 2 and 3 require connectivity and even worse a decdicated backend to support it. IMHO option 1 is often very good and underrated.

IOS:Call method when local notification is received in background

You're going to need to explicitly run your method in a UIBackgroundTask. See example below:

    UIApplication * application = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier background_task;
background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
}];

// your method call here

[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;

I'm doing something similar and this works for me. Let me know if thats not working for you.

How can I change a label with a date once the system date changes in iOS?

If you are only interested in being notified when midnight arrives (or a couple other cases), then you can override UIApplicationDelegate's - (void)applicationSignificantTimeChange:(UIApplication *)application method so that it causes your labels to update. Of course, you'll also want to update the labels whenever the application re-enters the foreground, since this method won't be called if your app is in the background when the date changes.

iPhone alarm using repeated local notifications

I am afraid you cannot accomplish this..reason being as you stated the 'Close' button. You won't be getting any call back in the app if Close button is tapped. Further even if you present notifications every 30 seconds, then there will be multiple notifications on the screen which user has to view or close. So the user experience will be crappy. I would recommended making it clear to the users that they can not set alarm with a custom sound more than 30 seconds.

How to send a localNotification at a specific time everyday, even if that time has passed?

Updated @Paulw11 's answer to Swift 3.0 and wrapped up in a function:

/// Set up the local notification for everyday
/// - parameter hour: The hour in 24 of the day to trigger the notification
class func setUpLocalNotification(hour: Int, minute: Int) {

// have to use NSCalendar for the components
let calendar = NSCalendar(identifier: .gregorian)!;

var dateFire = Date()

// if today's date is passed, use tomorrow
var fireComponents = calendar.components( [NSCalendar.Unit.day, NSCalendar.Unit.month, NSCalendar.Unit.year, NSCalendar.Unit.hour, NSCalendar.Unit.minute], from:dateFire)

if (fireComponents.hour! > hour
|| (fireComponents.hour == hour && fireComponents.minute! >= minute) ) {

dateFire = dateFire.addingTimeInterval(86400) // Use tomorrow's date
fireComponents = calendar.components( [NSCalendar.Unit.day, NSCalendar.Unit.month, NSCalendar.Unit.year, NSCalendar.Unit.hour, NSCalendar.Unit.minute], from:dateFire);
}

// set up the time
fireComponents.hour = hour
fireComponents.minute = minute

// schedule local notification
dateFire = calendar.date(from: fireComponents)!

let localNotification = UILocalNotification()
localNotification.fireDate = dateFire
localNotification.alertBody = "Record Today Numerily. Be completely honest: how is your day so far?"
localNotification.repeatInterval = NSCalendar.Unit.day
localNotification.soundName = UILocalNotificationDefaultSoundName;

UIApplication.shared.scheduleLocalNotification(localNotification);

}

how do I set an existing NSDate's time?


NSDate *date = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
[components setHour: 7];
[components setMinute: 59];
[components setSecond: 17];

NSDate *newDate = [gregorian dateFromComponents: components];

I use NSUIntegerMax instead of specific OR-combined bit masks because it's easier, but if you want to specify which data components to receive, be my guest.



Related Topics



Leave a reply



Submit