Notification alert not working - ios

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

Related

How to notify UILocalNotification for every two week [duplicate]

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

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

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/

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