I am making a timer app. The user sets the time and the app counts down from there. I have added a UILocalNotification, so that it pops up even when you aren't in the app to tell you the timer has finished:
IN MY VIEW CONTROLLER:
- (void)setupLocalNotifications {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
totalSeconds = (setHour * 60 * 60) + (setMinute * 60) + (setSecond);
NSDate *now = [NSDate date];
NSDate *dateToFire = [now dateByAddingTimeInterval:totalSeconds];
localNotification.fireDate = dateToFire;
localNotification.alertBody = #"Timer Done";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1; // increment
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:#"Object 1", #"Key 1", #"Object 2", #"Key 2", nil];
localNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
IN MY APPDELEGATE:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.;
ScrollViewController *sv = [[ScrollViewController alloc] init];
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
[self showAlarm:notification.alertBody];
NSLog(#"AppDelegate didFinishLaunchingWithOptions");
application.applicationIconBadgeNumber = 0;
}
self.window.rootViewController = sv; // Make tab bar controller the root view controller
[self.window makeKeyAndVisible];
return YES;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[self showAlarm:notification.alertBody];
application.applicationIconBadgeNumber = 0;
NSLog(#"AppDelegate didReceiveLocalNotification %#", notification.userInfo);
}
- (void)showAlarm:(NSString *)text {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Timer"
message:text
delegate:self
cancelButtonTitle:#"Stop Timer"
otherButtonTitles:nil];
[alertView show];
}
What happens is, I set a UILocalNotification to go off after the user-defined number of seconds has passed. However, my app allows you to pause the timer. When paused, the UILocalNotification will carry on, and go off after the seconds have passed. Is there any way to pause the local notification?
A local notification cannot be paused. If the timer is paused in your application,
you should cancel the notification, and create/schedule a new one if the timer
is resumed.
Related
i'm looking for work sound on notification with soundName in Objective-C on test project, but this doesn't work.
I used the code from how to create local notifications in iphone app answer, but sound don't work, i don't understant why :(.
In my ViewController.m
- (IBAction)startLocalNotification:(id)sender {
NSLog(#"startLocalNotification");
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
notification.alertBody = #"This is local notification!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 10;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
And in my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|
UIUserNotificationTypeSound categories:nil]];
}
[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
return YES;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:#"Notification" message:#"This local notification"
delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[notificationAlert show];
}
make sure your app is in background so you can hear sound otherwise you cannot able to hear the sound.
I am implementing local notifications in my iOS app. Actually, I have a implementation with a method when i have created a notification :
-(void)setAlarmAction: (NSDate *) notificationDay days: (NSNumber *) day alert : (NSNumber * ) priority name:(NSString * ) nameNotification {
NSDate *notificationDate = notificationDay;
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil) {
return;
}
localNotification.fireDate = notificationDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = [NSString stringWithFormat: #"Quedan %# dias" , day];
localNotification.alertAction = #"Ver Documento";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1; //aumenta en uno el numero de notificaciones
////////////
NSDictionary *infoDictionary = [NSDictionary dictionaryWithObject:#"notification" forKey:nameNotification]; //1º que se guarda , 2º con que clave
localNotification.userInfo = infoDictionary;
////////////
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:localNotification];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:nameNotification];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// Get list of local notifications
NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
for( UILocalNotification * local in localNotifications) {
NSLog(#"Notificacion: %# fecha :%#",local.alertBody,local.fireDate.description);
}
}
-(void) launchNotificacion:(Documento *)document {
self.txDate.text = document.text;
}
I want the app run in background and when it appear on the tabBar, i can push it and it go me to a concret view and launch a few data. I did it in the didFinishlaunchingWithOptions.
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//self.viewDocumentForm
VTDocumentFormViewController *VTDocument = [[VTDocumentFormViewController alloc] initWithNibName:#"VTDocumentFormViewController" bundle:nil];
UILocalNotification *notification = [launchOptions objectForKey:
UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
[self.window.rootViewController presentViewController: VTDocument animated:YES completion:nil];
Documento *reminderDocument = [NSEntityDescription insertNewObjectForEntityForName:#"Documento" inManagedObjectContext:self.managedObjectContext];
reminderDocument = [notification.userInfo objectForKey: key ];
[self.viewDocumentForm launchNotificacion:reminderDocument];
application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1;
}
// Override point for customization after application launch.
return YES;
}
But it doesn't work and i don't know the reason. Please, help.
didFinishLaunchingWithOptions: with UIApplicationLaunchOptionsLocalNotificationKey in launchOptions will be invoked only if app was suspended/killed before localNotification.fireDate reached.
If you'r app is just in background (not killed) or active you'll got
application:didReceiveLocalNotification: fired
Also i don't see in your didFinishLaunchingWithOptions: where did you register user notification settings (for iOS8)… smth like:
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
}
my requirement is to fire notification message like popup in iPad, message i am getting from web service. i am calling web service after certain time interval and only if user to be notified then only i have to show notification.
my code is not working, i want like pop up. my code is below:
- (void)scheduleNotificationWithInterval:(NSString *)Notificationmsg
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
return;
localNotification.fireDate = [NSDate date];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = #"MyApp!";
localNotification.repeatInterval = nil;
localNotification.alertBody = [NSString stringWithFormat:NSLocalizedString(Notificationmsg, nil)];
localNotification.alertAction = NSLocalizedString(#"View Details", nil);
localNotification.applicationIconBadgeNumber = 1;
/*
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:#"Object 1", #"Key 1", #"Object 2", #"Key 2", nil];
localNotification.userInfo = infoDict;*/
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
Please help me.
Try this one:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
// I recieved a notification
}
put this method in Appdelegate class.
When Your fire localnotification and app is open Active state it will call this method and if app is in background mode it will show notification i.e what you style you have set for your app.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
NSLog(#"lcoal:%#",[notification userInfo]);
UIAlertView *al=[[UIAlertView alloc]initWithTitle:#"Challenge gehaald!" message:#"Gefeliciteerd, je hebt deze bonus challenge succesvol afgerond." delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[al show];
}
}
Hi I'm trying to set local notifications to fire after 24 hours but for testing only I have put it down to 30 sec.
Problem is that the local notifications only fires ONCE (after the 30sec) and then... nothing
here is my code:
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSString *path = [[NSBundle mainBundle] pathForResource:
#"myTipOfTheDay" ofType:#"plist"];
NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSMutableArray *plistArray = plistDict[#"tipOfTheDay"];
int randTip = arc4random() % plistArray.count;
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:30]; //60*60*24 for 24 hours
**EDIT:** //notification.repeatInterval = NSMinuteCalendarUnit; This only repeats the SAME message every min and does not fire up a random string from my plist....
notification.alertBody = plistArray[randTip];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
//cancel notifications if app is active
}
Apparently we cannot change the alertBody on demand every time the repeatInterval is getting called.
So wrong....
Anyway here is the code I using now:
- (void)applicationDidEnterBackground:(UIApplication *)application {
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = #"Hey, I've got a tip for you...";
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:60*60*24];
notification.repeatInterval = NSDayCalendarUnit; //NSMinuteCalendarUnit | NSDayCalendarUnit
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
application.applicationIconBadgeNumber = application.applicationIconBadgeNumber + 1;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if (application.applicationIconBadgeNumber > 0) { //check if user has been notified...
even if user cleared the notification, an alert will still show with the Tip of The Day...
NSString *path = [[NSBundle mainBundle] pathForResource:
#"myTipOfTheDay" ofType:#"plist"];
NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSMutableArray *plistArray = plistDict[#"randTip"];
int randV = arc4random() % plistArray.count; //random selection of string
UIAlertView *tipOfTheDaAlert = [[UIAlertView alloc] initWithTitle:#"Did you know that..."
message:plistArray[tipOfDay]
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[tipOfTheDaAlert show];
application.applicationIconBadgeNumber = 0; //reset the icon badge to zero
}
}
I've created a old uilocalnotification, now, I want to update it, I searched on Google but there are no way to update the local notification. So, I decided to cancel it and create a new local notication:
- (void) cancelLocalNotificationByUserInfo: (NSDictionary *)dictInfo
{
UIApplication *application = [UIApplication sharedApplication];
NSArray *notifArr = [application scheduledLocalNotifications];
for (int i=0; i<notifArr.count; i++)
{
UILocalNotification* theNotif = (UILocalNotification *)[notifArr objectAtIndex:i];
if ([theNotif.userInfo isEqual:dictInfo])
{
[application cancelLocalNotification:theNotif];
}
}
}
- (void) addLocalNotification: (NSDate *) fireDate soundName: (NSString *) soundName
alertBody: (NSString *) alertBody infoDict: (NSDictionary *)infoDict
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = fireDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = alertBody;
// Set the action button
localNotif.alertAction = #"Show me";
localNotif.hasAction = YES;
localNotif.applicationIconBadgeNumber = 1;
localNotif.soundName = soundName;
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
But, everytime, I execute the addLocalNotification method, the old local notification is fired immediately. Please help me!