Scheduled local notification and timezone changes in iOS - ios

I have this method in my app to fire scheduled local notifications:
- (void)scheduleNotificationForTime:(long long)atTime
{
NSDate *fireDate = [DatesMngr getDateFromLongLong:atTime];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = fireDate;
localNotif.timeZone = [NSTimeZone localTimeZone];
localNotif.alertBody = #"Active Status";
localNotif.alertAction = #"View Details";
localNotif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
It works fine if user does not change her timezone in her device's Settings, but the notification is not launched if she changes the timezone after it has been already scheduled but not launched yet. I've also tried with [NSTimeZone defaultTimeZone] with no success. Is there any way to detect these timezone changes and refresh the fireDate for already scheduled local notifications?
Thanks

You can do it in 2 ways
implement the following method in your appdelegate
(void)applicationSignificantTimeChange:(UIApplication *)application
2
. Register for UIApplicationSignificantTimeChangeNotification in notificationcenter
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleTimeChange:)];

Related

Local notification at multiple times like alarm in ios

I am creating an alarm app. I want user set time and Local notification come at that time. For this i am using following code:-
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = _datePicker.date;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"This is the Alert-Body Text";
localNotif.alertAction = #"Button-Text";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
But the problem is notification comes only once and how i set multiple times for local notification so that it comes for every alarm.

Difference between push notification and nsnotifcation centre in iOS sdk

I have question about push notification and the Notification Center. I have built an app and now I want to support notification the the time hits 0 I'm doing like countdown app shows the release date for games and title.
Should I use nsnotifcation center or push notification
You dont need Push Notifications you can acheive that with UILocalNotification. For Push Notifications you need coordination with server but for UILocalNotification you can do it inside your application so better try to use UILocalNotification
For simple UILocalNotification
UILocalNotification* localNotification = [[UILocalNotificationalloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertBody = #"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
For UILocalNotification with timers
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = self.itemText.text;
localNotification.alertAction = #"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
you can study in detail in here http://www.appcoda.com/ios-programming-local-notification-tutorial/

iOS - NSLocalNotification- set notification every other day

I want to set a Local Notification every other day (multiplication on NSDayCalendarUnit), How can I achieve this?
So far, I've done this:
- (void)scheduleLocalNotication {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// Set the fire date/time
[localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
localNotification.applicationIconBadgeNumber = 1;
// Setup alert notification
[localNotification setAlertAction:#"Open App"];
[localNotification setAlertBody:#"setAreltBody"];
[localNotification setAlertBody:#"You had set a Local Notification on this time"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
[localNotification setHasAction:YES];
localNotification.repeatInterval = NSDayCalendarUnit;
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:localNotification];
}
Can I change this line:
localNotification.repeatInterval = NSDayCalendarUnit;
to something like this:
localNotification.repeatInterval = NSDayCalendarUnit * 2;
I guess not, since NSDayCalendarUnit is a typedef, so how can I do this?
I don't think this is possible using the repeatInterval property of UILocalNotification, for the same reason that you already pointed out, NSCalendarUnit is an enum, so you can only use the following values:
An alternative option would be to schedule a number of notifications in a loop and always add 24 hours to the previous fire date.

Repeat Local notifications for selected days at the same time

In my app I am using UILocalNotifications. I want a functionality in which user can repeat alarms for specific days. Like(Monday,Wednesday,Sunday) at say 9pm. How can i do this.
Thanks in Advance
I am using the following code
UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:alertBody];
[notification setFireDate:[myDatePicker date]];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = notification.applicationIconBadgeNumber+1;
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
There's a repeatInterval property on the noitification , use that.
Err... ...why don't just schedule three notifications for the three days, each with a week as repreatInterval?
Try this code
NSDate *AlarmTime1 =[[NSDate date] dateByAddingTimeInterval:expiretime];
UIApplication *app1 = [UIApplication sharedApplication];
UILocalNotification *notifyAlarm1 = [[UILocalNotification alloc] init];
if (notifyAlarm1) {
notifyAlarm1.fireDate = AlarmTime1;
notifyAlarm1.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval=0;
notifyAlarm.soundName= #"applering_zwnNYFkA.wav";
notifyAlarm1.soundName=#"stop.wav";
notifyAlarm1.alertBody =NSLocalizedString(#"EXPIRE_DESC", nil);
[app1 scheduleLocalNotification:notifyAlarm1];

Register for Local Notification

I am developing an IOS application with phonegap and need to set local notification for it which will repeat on every friday and specified time
Also there is requirement that user will decide to receive or not the local notification
I recommend you read the following article on the topic which i found very helpful
http://useyourloaf.com/blog/2010/7/31/adding-local-notifications-with-ios-4.html
- (void)scheduleNotification {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.fireDate = [datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Body";
notif.alertAction = #"AlertButtonCaption";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
}
This is just a basic outline of how it works but starting from this you should be able to schedule a notification.

Resources