UILocalNotification won't fire - iphone

I'm writing an alarm clock iOS app. It's my first time using UILocalNotification. I'm getting the date from a date picker. I've formatted the dates to check whether my function was being passed the proper date, it was. I checked all of the needed properties for a UILocalNotification and I have them all and my notification still won't fire. Any ideas as to why? Thanks for the help.
#import "BIDAlarmViewController.h"
#interface BIDAlarmViewController ()
#end
#implementation BIDAlarmViewController
#synthesize datePicker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)setReminderUsingDateFromDatePicker: (id)sender{
[self scheduleNotificationForDate: datePicker.date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd 'at' HH:mm"];
NSString *formattedDateString = [dateFormatter stringFromDate:datePicker.date];
NSLog(#"Button Pressed.. date: %#", formattedDateString);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alarm activated"
message:#"Alarm has been set"
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
-(void) scheduleNotificationForDate: (NSDate*)date {
UILocalNotification *alarm = [[UILocalNotification alloc] init];
if (alarm) {
alarm.fireDate = date;
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = 0;
alarm.soundName = #"alarmsound.caf";
alarm.alertBody = #"Test message...";
[[UIApplication sharedApplication] scheduleLocalNotification:alarm];
}
}
#end

http://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveLocalNotification:
Make sure you've implemented the referenced method in your app delegate like so:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(#"Notification fired"!);
}
Notes from Apple on implementing this method:
Local notifications are similar to remote push notifications, but
differ in that they are scheduled, displayed, and received entirely on
the same device. An application can create and schedule a local
notification, and the operating system then delivers it at the
schedule date and time. If it delivers it when the application is not
active in the foreground, it displays an alert, badges the application
icon, or plays a sound—whatever is specified in the
UILocalNotification object. If the application is running in the
foreground, there is no alert, badging, or sound; instead, the
application:didReceiveLocalNotification: method is called if the
delegate implements it.
The delegate can implement this method if it wants to be notified that
a local notification occurred. For example, if the application is a
calendar application, it can enumerate its list of calendar events to
determine which ones have due dates that have transpired or are about
to transpire soon. It can also reset the application icon badge
number, and it can access any custom data in the local-notification
object’s userInfo dictionary.

Related

iOS Alarm Clock

I have created a simple alarm notification App through which I can get real time, set alarm on or off, and play a single tone audio. But I need to play a sound which should start with a class VOID.
Below is the code:
To get and start alarm notification:
- (void)viewDidLoad {
[super viewDidLoad];
dateTimerPicker.date = [NSDate date];
}
- (void)presentMessage:(NSString *)message {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Hello!"
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
}
- (void)scheduleLocalNotificationWithDate:(NSDate *)fireDate {
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = fireDate;
notification.alertBody = #"Time to wake up!!";
notification.soundName = #"PhoneOld.mp3";
[self playPause];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
- (IBAction)alarmSetOn:(id)sender{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.dateStyle = NSDateFormatterShortStyle;
NSString *dateTimeString = [dateFormatter stringFromDate:dateTimerPicker.date];
NSLog(#"Alarm Set: %#", dateTimeString);
[self scheduleLocalNotificationWithDate:dateTimerPicker.date];
[self presentMessage:#"Alarm ON!"];
}
- (IBAction)alarmSetOff:(id)sender {
NSLog(#"Alarm Off");
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self presentMessage:#"Alarm OFF!"];
}
This is my VOID:
- (void)playPause {
RADAppDelegate *appDelegate = (RADAppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.radiosound == 0){
[appDelegate.radiosound play];
} else {
[appDelegate.radiosound pause];
}
}
How can I set the alarm to start playing the radiosound if is rated 0, like a:
notification.soundName = [self playPause];
But I know this is a NSString.
You don't need to assign a sound name to scheduled notification, just invoke the playPause method and get the name of sound file from notification, as shown below and just assign it to NSString and set property to it in appDelegate and access it to play that file.
AppDelegate.h
#property (nonatomic,strong) NSString *nsStr_soundFile;
AppDelegate.m
#synthesize nsStr_soundFile;
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
//Give call to play sound method.
self.nsStr_soundFile=notification.soundName;
VOID *obj=[VOID alloc]init];
[obj playPause];
}
You can make a trick with opting out of iOS multitasking by setting in your app .plist file this key UIApplicationExitsOnSuspend to YES as written here
When an app opts out, it cycles between the not-running, inactive, and
active states and never enters the background or suspended states.
An app that runs in the pre-multitasking compatibility mode keeps
running when the user locks the device while the app is in the
foreground. All the app has to do is wait for the alarm time and
execute its custom code.

iOS: Local Notifications Don't Fire On Time

The issue is that my notifications aren't going through when they are set up to. I put a date picker in the nib and a button underneath it. The user is supposed to set the date in the picker, and clicking the button set up the notification to fire 60 hours before the date. So, it looks like I'm just having issues with getting the firedate to recognize the date in the date picker. Here is the code, it is in my view controller:
- (IBAction)scheduleNotifButton:(id)sender {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDate *selectedDate = [self.datePicker date];
localNotif.fireDate = [selectedDate dateByAddingTimeInterval:-60*60*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Event is in three days!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Event scheduled."
message:#"You will be notified three days before the event."
delegate:nil
cancelButtonTitle:#"Okay."
otherButtonTitles:nil];
[alert show];
}
And here is some additional code if that helps, this is more code from my view controller that deals with saving the date in the picker that the user entered:
- (IBAction)dateChanged:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *selectedDate = [self.datePicker date];
[defaults setObject:selectedDate forKey:#"ImportantDatesViewController.selectedDate"];
[defaults synchronize];
}
- (void)viewDidLoad {
NSDate *storedDate = [[NSUserDefaults standardUserDefaults]
objectForKey:#"ImportantDatesViewController.selectedDate"];
if (storedDate == nil) {
storedDate = [NSDate date];
}
[self.datePicker setDate:storedDate animated:NO];
}
I've been digging through this code for 3 days now and cannot figure out why my notifications aren't going through when they are supposed to. Any help is very much appreciated, thank you!
Do you understand that UILocalNotifications and remote notifications are different things?
In didRegisterForRemoteNotificationsWithDeviceToken you get the device token to be used for remote notifications. Remote notifications are sent from a server so you have to save that token on your server and then use your server to send any remote notifications.
Why don't you change this:
localNotif.fireDate = [eventDate dateByAddingTimeInterval:-630*60];
to this:
localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
Then the local notification should go off in a minute. Maybe the problem is the date that you are setting.
How do you know your notifications aren't working?
Local and push notifications are different. If your app is currently active, a local notification won't actually show an alert message. It will just call a UIApplicationDelegateMethod
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

How to add Multiple events to iphone calendar from iphone/ipad app?

I want your help my friends. Am developing iphone/ipad universal application. I want to add multiple events that is selected by user(it may 1-50 events). This app will add the events to iphone calendar. The events and event dates may differ. How to add multiple events to calendar without user interaction? I know well to add single event to iphone/ipad calendar but, i dont know to add multiple events? Please, help me friends.. I searched in google but didnt get the answer? Please.. Thanks in advance.
Thanks to read my poor english.
Yuva.M
Probably you have to store all your event objects in an array, then loop through it and add one-by-one to iPhone calendar.
.h file
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>
// EKEventStore instance associated with the current Calendar application
#property (nonatomic, strong) EKEventStore *eventStore;
// Default calendar associated with the above event store
#property (nonatomic, strong) EKCalendar *defaultCalendar;
.m file
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Calendar event has called
self.eventStore = [[EKEventStore alloc] init];
// Check access right for user's calendar
[self checkEventStoreAccessForCalendar];
}// end viewDidLoad
- (BOOL) addAppointmentDateToCalender:(NSDate *) appointment_date {
self.defaultCalendar = self.eventStore.defaultCalendarForNewEvents;
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
// Doctor Name
NSString *doctorName = [objSpineCustomProtocol getUserDefaults:#"doctorName"];
// Title for the appointment on calender
NSString *appointment_title = [NSString stringWithFormat:#"Appointment with Dr.%#", doctorName];
event.title = appointment_title;
//[NSDate dateWithString:#"YYYY-MM-DD HH:MM:SS ±HHMM"], where -/+HHMM is the timezone offset.
event.startDate = appointment_date;
NSLog(#"Start date of appointment %#",event.startDate);
NSDate *end_date_appointment = [[NSDate alloc] initWithTimeInterval:1800 sinceDate:appointment_date];
event.endDate = end_date_appointment;
NSLog(#"End date of appointment %#",event.endDate);
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
return true;
}// end method add_appointment_date_to_calender
-(void)checkEventStoreAccessForCalendar {
NSLog(#"Method: checkEventStoreAccessForCalendar");
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
switch (status) {
// Update our UI if the user has granted access to their Calendar
case EKAuthorizationStatusAuthorized: [self accessGrantedForCalendar];
break;
// Prompt the user for access to Calendar if there is no definitive answer
case EKAuthorizationStatusNotDetermined: [self requestCalendarAccess];
break;
// Display a message if the user has denied or restricted access to Calendar
case EKAuthorizationStatusDenied:
case EKAuthorizationStatusRestricted:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Privacy Warning" message:#"Permission was not granted for Calendar"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
break;
default:
break;
}
}// end of emethod checkEventStoreAccessForCalendar
// Prompt the user for access to their Calendar
-(void)requestCalendarAccess
{
[self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (granted) {
AppointmentViewController* __weak weakSelf = self;
// Let's ensure that our code will be executed from the main queue
dispatch_async(dispatch_get_main_queue(), ^{
// The user has granted access to their Calendar; let's populate our UI with all events occuring in the next 24 hours.
[weakSelf accessGrantedForCalendar];
});
}
}];
}
// This method is called when the user has granted permission to Calendar
-(void)accessGrantedForCalendar
{
NSLog(#"Method: accessGrantedForCalendar");
// Let's get the default calendar associated with our event store
self.defaultCalendar = self.eventStore.defaultCalendarForNewEvents;
}// end of method accessGrantedForCalendar

Setting Local Notifications on a new thread?

I need to know if it is possible to create a new thread to handle setting local notifications.
My app depends heavily on these notifications, so I want to make the app work while the phone sets the notifications.
Example:
(now)
you launch the app, the app hangs at the splash screen to set the local notifications, then it launches.
(I want)
The app launches and is usable while the Local notifications are set.
I need some sample code, too, please :)
(for the record, i am setting 60 local notifications each time the app enters foreground for my own reasons...)
Thanks!!
Yes this can be done, I do it all the time:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the navigation controller's view to the window and display.
[NSThread detachNewThreadSelector:#selector(scheduleLocalNotifications) toTarget:self withObject:nil];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
-(void) scheduleLocalNotifications
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (int i = 0; i < 60; i++)
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
NSDate *sleepDate = [[NSDate date] dateByAddingTimeInterval:i * 60];
NSLog(#"Sleepdate is: %#", sleepDate);
localNotif.fireDate = sleepDate;
NSLog(#"fireDate is %#",localNotif.fireDate);
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(#"This is local notification %i"), i];
localNotif.alertAction = NSLocalizedString(#"View Details", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(#"scheduledLocalNotifications are %#", [[UIApplication sharedApplication] scheduledLocalNotifications]);
[localNotif release];
}
[pool release];
}
Taken from a project I am working on now, I can confirm that It works as expected.
EDIT:
Example was leaking in scheduleLocalNotifications because handling the NSAutoreleasePool was missing – now it's added to the example.
One way to do threads is with is with performSelectorInBackground.
For example:
[myObj performSelectorInBackground:#selector(doSomething) withObject:nil];
You should note, however, that Apple is pretty strongly recommending that you use higher-level concepts like NSOperations and Dispatch Queues instead of explicitly spawning threads. See the Concurrency Programming Guide

local notification "didReceiveLocalNotification" calls twice

I am handling local notifications using:
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
And to schedule a local notification:
- (void)scheduleNotificationWithInterval:(int)minutesBefore {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
NSDate *fireDate = [NSDate date];
localNotif.fireDate = [fireDate dateByAddingTimeInterval:minutesBefore*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatInterval = kCFCalendarUnitMinute;
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(#"LocalEvent notification in %i minutes.", nil),minutesBefore];
localNotif.alertAction = NSLocalizedString(#"View Details", nil);
localNotif.applicationIconBadgeNumber = 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:#"This is dict, you can pass info for your notification",#"info",nil];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
NSLog(#"Event scheduled");
}
When I receive a notification, didReceiveLocalNotification: is called twice.
Am I doing something wrong?
Please help.
Thanks.
I think there is a known bug in the simulator, that fires the delegate notification method twice. It should not happen on the device, tethered to XCode or not.
i was also facing the same problem and the solution which i find is that write this code in didReceiveLocalNotification
if (state == UIApplicationStateActive) {
NSLog(#"UIApplicationStateActive");
}
else if(state == UIApplicationStateInactive){
NSLog(#"UIApplicationStateInActive");
}
here in these condition i just write the code which i want my application to do on notification , in Active mode and in inactive mode
I suspect that the notification is being retriggered as long as its in the same second still. I fixed it by setting the fireDate to nil in the handler:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:notification.alertAction message:notification.alertBody delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
notification.fireDate = nil;
}
I had the same issued. It was caused by calling 'registerUserNotificationSettings' twice in the AppDelegate's 'didFinishLaunchingWithOptions.' However, simply removing the duplicate call did not fix the problem yet. I had to delete the app and then rebuild. Only then did the double local notification issue get fixed.