Alert Action Not Showing - iphone

I have my notification, but for some reason it still doesnt come up as an alert view.
Here it is :
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification) {
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = #"Notification";
notification.alertAction = #"View";
[app scheduleLocalNotification:notification];
}

If you want to view your local notification as a UIAlertView and not a banner, you'll have to test your app on an actual device and change your app's notification 'Alert Style' in Settings. Since, as you said, there is no 'Notifications' option in the simulator's Settings, it will always default to a banner.

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.

How to initialize local notification?

I want to implement local notification in my clock app.Basically i want that a music file should be played after every half an hour like in ship's clock in which chimes are played after every 30 minutes.
Can anyone give rough idea as how i can implement this functionality even when the app enters in background?
I recently used the Local notification stuff and used the following functions
//Setting up the Local Notifications
for (int i= 1 ; i<=10; i++) { //We here set 10 Notification after every 30 minutes from now you can modify it accordingly
NSDate *scheduled = [[NSDate date] dateByAddingTimeInterval:60*30*i]; //These are seconds
NSDictionary* dataDict = [NSDictionary dictionaryWithObjectsAndKeys:scheduled,FIRE_TIME_KEY,#"Background Notification received",NOTIFICATION_MESSAGE_KEY,nil];
[self scheduleNotificationWithItem:dataDict];
}
Where scheduleNotificationWithItem is defined as
- (void)scheduleNotificationWithItem:(NSDictionary*)item {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil) return;
localNotification.fireDate = [item valueForKey:FIRE_TIME_KEY];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = [NSString stringWithFormat:NSLocalizedString(#"%#", nil), [item valueForKey:NOTIFICATION_MESSAGE_KEY]];
localNotification.alertAction = NSLocalizedString(#"View Details", nil);
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.userInfo = item;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
}
Finally you can handle these notifications as
You can handle these notifications as follows
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// Do the required work you can obtain additional Info via notification.userInfo which happens to be a dictionary
}
reading the developer documentation will help you more to understand the stuff.Hope it helps
You can use UILocalNotifications and set their 'firedate', according to your requirement and then schedule the notification. These notifications doesn't bother whether your app is running or is in background they will always show up like an alertview.

How can I use local notifications to wake my app?

i want that my app will run in the background and do check on somethings.
and if so the local notification will show.
i use this code in the method that check:
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSArray *oldNotifications = [app scheduledLocalNotifications];
if ([oldNotifications count] > 0) {
[app cancelAllLocalNotifications];
}
if (notification == nil)
return;
NSDate *notificationDate = [NSDate dateWithTimeIntervalSinceNow:10];
notification.fireDate = notificationDate;
notification.timeZone = [NSTimeZone systemTimeZone];
notification.alertBody = #"Test Body";
[app scheduleLocalNotification:notification];
[notification release];
the problem is that when the app is in the foreground it show the alert, but if the app is in the background the notification not show to the screen.
the check that i do is running on uiwebview and reload every 10 seconds, so he need to run in the background too.
Your app won't run in background, except certain cases. Look in to this How to run the application in the background? post.

how to set uilocalnotification firedate for a fixed date?

i want to set localnotification say for a program which starts at 6:00 pm.For that i have taken the time in a date variable and i am comparing it with current date from system.
Say setDate is for fixed date i.e 6.00 pm so i have to set firedate such that it shows the notification before 30 mintes the program starts. The examples i have seen in that the firedate is set according to currentdate.
Can someone tell me how can i set firedate according to my fixed date??
You fire the local notification this way
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [NSDate date];// Now here you can manage the fire time.
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = #"BusBuddy";
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"You are near to reach the Bus Stop" forKey:#"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
This line of code will work for you. you just need to provide the date time for this
localNotif.fireDate = [NSDate date];
And now for formatting your date time you can refer to these links
iphonedevelopertips.com
developer.apple.com, CFDatesAndTimes
developer.apple.com, DataFormatting
Well then you can handle your local notification in application delegate file when ever you get the notification.
e.g. here is the delegate method which is fired everytime when you get the local notification.
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
//Handle the notificaton when the app is running
NSLog(#"Recieved Notification %#",notif);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Hey Neha" message:#"Sanjay wants to be your friend " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertView show];
[alertView release];
SystemSoundID bell;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"WhoopFlp" ofType:#"wav"]], &bell);
AudioServicesPlaySystemSound (bell);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
i=0;
}
}
So what I am doing here is simply showing the alert and playing a system sound when my local notification occurs.
So now what you want is to navigate to the page in the program where you were when you get the local notification.So simply in this delegate method you need to allocate your view controller and need to push the view controller to that view where you want to be.
That would solve your problem.
Well i had a similar kind of problem what i wanted is to show the notification in the background and in the front end as well, so writing the 2 different methods in my app was not worthful.so i handled it this way in the delegate method which will show the notification in the front end as well.
Good luck to you.
localnotification =[[UILocalNotification alloc]init];
[localnotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:[lodatepicker countDownDuration]]];
[localnotification setAlertAction:#"Launch"];
[localnotification setHasAction: YES];
[localnotification setAlertBody:[lotextview text]];
// [localnotification setSoundName:musicString];
localnotification.timeZone = [NSTimeZone defaultTimeZone];
[localnotification setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]+1];
[[UIApplication sharedApplication] scheduleLocalNotification:localnotification];

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.