How to notify UILocalNotification for every two week [duplicate] - ios

This question already has answers here:
how to set local notification for every two weeks
(2 answers)
Closed 6 years ago.
Good Morning,
How to notify localNotification for every two weeks (14days) a notification.
- (void)applicationDidEnterBackground:(UIApplication *)application {
timer = [NSTimer scheduledTimerWithTimeInterval:60*60*24*14 target:self selector:#selector(getNotifiedForTwoWeeks:) userInfo:nil repeats:YES];
}
-(void)getNotifiedForTwoWeeks:(id)userinfo{
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate date];
localNotification.alertBody = #"Notification Message";
localNotification.alertAction = #"Show me the item";
localNotification.timeZone = [NSTimeZone timeZoneWithName:#"GMT"];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Please let me know is this implementation is correct or not?
Is there any alternative way i can do the best for notifying a LocalNotification message for every two weeks.
Your valuable inputs are appreciated!

When your app will enter to background the system will suspend it after some time and your NSTimer will not schedule.
So you can schedule for every 14 days notification through declare a fireDate and repeatInterval properties in UILocalNotification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateByAddingTimeInterval:60*60*24*14];
localNotification.alertBody = #"Notification Message";
localNotification.alertAction = #"Show me the item";
localNotification.timeZone = [NSTimeZone timeZoneWithName:#"GMT"];
localNotification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Related

Notification alert not working

ViewController.m
- (IBAction)setAlarm:(id)sender {
[defaults setBool:YES forKey:#"notificationIsActive"];
[defaults synchronize];
//self.message.text=#"Notifications Started";
NSTimeInterval interval;
interval = 10;
NSLog(#"time interval.....%f",interval);
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:interval]; //Enter the time here in seconds.
localNotification.alertBody= #"This is message Users will see";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval= NSCalendarUnitDay;//NSCalendarUnitMinute;
//Repeating instructions here.
localNotification.soundName= UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
I use this code for notification alert with button click. This code works with my app for several times to give notification alert. But suddenly it stops working. Please help.
If you're on iOS 10 & above, Apple Doc says
UILocalNotification is deprecated in iOS 10. Use UNNotificationRequest
instead. A UILocalNotification object specifies a notification that an
app can schedule for presentation at a specific date and time.
Reference Links:
UserNotifications
Local Notification in iOS 10 for Objective-C and Swift 3
You need to use it for iOS 10 and later.
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = #"Don't forget";
content.body = #"Buy some milk";
content.sound = [UNNotificationSound defaultSound];
At a certain time
NSDateComponents* date = [[NSDateComponents alloc] init];
date.hour = 7;
date.minute = 0;
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger
triggerWithDateMatchingComponents:date repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:#"MorningAlarm" content:content trigger:trigger];

Showing notifications while app in background

I need to show certain local notifications while app is in the background. How can I do it, without the help of NSNotificationCenter?
Andrey Chernukha in first comment is right.
Here is the code to implement simple Local Notification:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = 0;
NSDate *now = [NSDate date];
NSDate *dateToFire = [now dateByAddingTimeInterval:10];
localNotification.fireDate = dateToFire;
localNotification.alertBody = #"Test local notification";
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

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/

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];

Adding seconds to a self.timepicker date (for localnotification purposes)

I want to know if it is possible to add seconds to a local notification? I am trying to create a loop to schedule local notifications 30 seconds after each others. So, in the loop below, can I keep on "delaying" the firedate 30 seconds after each other. I don't know if that question makes sense, but that's the best I can describe my problem as. Think of it as a 30 second interval, but manually scheduling each notification.
for (notifs = 1, notifs <= 64, notifs ++)
{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [self.timePicker date];
//can i write it like [self.timePicker date] + 30000?
localNotification.soundName = #"notifsound.caf";
localNotification.alertBody = #"Wake Up!!!";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
}
Thanks.
You could use NSDate's dateByAddingTimeInterval: and just pass the current iteration number multiplied by 30.
for (notifs = 1; notifs <= 64; notifs ++)
{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[self.timePicker date] dateByAddingTimeInterval:notifs * 30.0];
localNotification.soundName = #"notifsound.caf";
localNotification.alertBody = #"Wake Up!!!";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

Resources