After goggling for 2 days i couldn't find any solution as if its clear to everyone (but me) !
I need the:
Alert.applicationIconBadgeNumber = x
to be updated in background each time the notification fires, I am repeating the notify by:
notif.repeatInterval = NSMinuteCalendarUnit
Repeating is working fine every 1 m. when the app goes in background, but the BadgeNumber dosent get updated, it takes the 1st updated date value only.
I am calling the scheduleNotification method by viewDidLoad
Here is my full code:
- (void)scheduleNotification {
UILocalNotification *notif;
notif = [[[UILocalNotification alloc] init] autorelease];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.fireDate = [[NSDate date] dateByAddingTimeInterval:5];
notif.repeatInterval = NSMinuteCalendarUnit;
NSInteger BadgeNumber = [self BadgeNumber];
NSInteger *BadgeNumberPointer = &BadgeNumber;
NSString *BadgeNumberString = [NSString stringWithFormat:#"%i", BadgeNumber];
notif.applicationIconBadgeNumber = *BadgeNumberPointer;
notif.alertBody = BadgeNumberString;
notif.alertAction = #"Hello";
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
-(int)BadgeNumber{
NSDate *currentDateUpdate = [[NSDate alloc] init];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"dd"];
NSString *dateCheckUpdate = [formatter2 stringFromDate:currentDateUpdate];
NSInteger dateCheckUpdateInt = [[dateCheckUpdate substringWithRange:NSMakeRange(0, 2)] integerValue];
int BadgeNumber = dateCheckUpdateInt;
return BadgeNumber;
}
Kindly advice how to fix it,, thanking all of you.
Because the operating system copies the notification when scheduled, the data in the notification is not updated, therefore the application badge number doesn't change.
Maybe if you don't repeat the notification but generate your own new notification for each time interval it will give you the behavior you need. The only way I can think of generating notifications like that is to post a bunch of notifications in your scheduleNotification method, and then remember to delete the notifications when the user responds in the proper way. Since the OS only remembers the next chronologically scheduled 64 notifications, you could only schedule about an hour's worth. Since your badge number seems to be the current date, you could check the time and only bother with setting so many notifications if you're within an hour of midnight.
I don't understand what you are trying to accomplish by nagging the user so often, nor telling them the date in the badge number. Any app that bothered me so much or misused the badge number so would quickly get deleted from my iOS devices. Maybe rethinking what you are trying to accomplish may direct you to a better solution.
I know this is already answered, but you could use NSUserDefaults as a means of caching the badge count. Then in applicationIconBadgeNumber you can just use something like this:
notif.applicationIconBadgeNumber = ([NSUserDefaults standardUserDefaults] integerForKey:#"badgeCount"] + 1);
and then you could just reset it when the user responds accordingly.
Related
I give a custom sound to local notification, but it is not working.
notif.soundName = #"sound.caf";
it's play default sound why?
Thanks.
Maybe you don‘t add the sound file (*.caf) in Xcode project: Build Phases/Copy Bundle Resources.
I have done notification successfully with the code below.
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
/* NSDateFormatter *formatDate=[[NSDateFormatter alloc]init];
[formatDate setDateFormat:#"MM/dd/yyyy"];
[formatDate setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *strDate=[formatDate dateFromString:txtDate.text];
*/
localNotif.fireDate = itemDate;
//localNotif.timeZone = [NSTimeZone systemTimeZone];
NSLog(#"the item date is %#",localNotif.fireDate);
// Notification details
//localNotif.alertBody = [eventText text];]
localNotif.alertBody=strEvent;
// Set the action button
localNotif.alertAction = #"View";
//localNotif.soundName = #"Acoustic Noodling 01.caf";
localNotif.soundName=#"alarm1.aif";
About playing custom sounds in Local notifications
Make sure the sound is actually in your app’s bundle, is in the correct format (linear PCM or IMA4
Assume that notification is an object of type UILocalNotification.
notification.soundName = #"sound.caf";
If this doesn't work initially then you may refer to this post and there in refer to This Answer That tells us to use sound file of the correct format (i.e. either Linear PCM or IMA4).
Just use relative path of your file rather than providing absolute or full path.
For example:
localNotification.soundName = #"../Documents/recordedFileName.caf";
Also, keep in mind that recordDuration of file should be less than or equal to 29 seconds.
That's it, hope that answered.
I am working on App which will set an alarm on ios for a time depending on user input.
Meaning: if a user selects row 1 of table then it will look into dictionary (which may say 20 minutes),,, then it should set an alarm in ios for (currrent time+ 20 minutes).
Can someone please tell me the best way to approach this.
You can use UILocalNotification:
UILocalNotification *local = [[UILocalNotification alloc] init];
// create date/time information
local.fireDate = [NSDate dateWithTimeIntervalSinceNow:20*60]; //time in seconds
local.timeZone = [NSTimeZone defaultTimeZone];
// set notification details
local.alertBody = #"Alarm!";
local.alertAction = #"Okay!";
local.soundName = [NSString stringWithFormat:#"Default.caf"];
// Gather any custom data you need to save with the notification
NSDictionary *customInfo =
[NSDictionary dictionaryWithObject:#"ABCD1234" forKey:#"yourKey"];
local.userInfo = customInfo;
// Schedule it!
[[UIApplication sharedApplication] scheduleLocalNotification:local];
[local release];
Hi I working on a reminder application.
I need to display a reminder alert after some particular time.
But not at the time we have set in date picker.
Just like I have a button 'Remind in 10 Mins'
-(IBAction)ReminderClick:(id)sender
{
}
When user press the button , After 10 mins it needs to display an alert.
You need to use UILocalNotification for this Function
Code is look like
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];
NSDate *date1=[fire dateByAddingTimeInterval:60];
notifyAlarm.fireDate = date1;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
//notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval =NSWeekCalendarUnit ;
notifyAlarm.soundName =soundString;
notifyAlarm.alertBody =snoozeBody;
notifyAlarm.userInfo=snoozeDict;
//notifyAlarm.alertLaunchImage=#"in.png";
[app scheduleLocalNotification:notifyAlarm];
and You can follow thos tutorial for this
http://www.icodeblog.com/tag/uilocalnotification/
http://blog.mugunthkumar.com/coding/iphone-tutorial-scheduling-local-notifications-using-a-singleton-class/
http://www.iostipsandtricks.com/ios-local-notifications-tutorial/
http://www.youtube.com/watch?v=tcVoq488-XI
You have to use UILocalNotificataion for this
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.
I'm trying to build an alarm clock similar to the Alarm Clock Pro and the Nightstand application that are currently in the app store. Each of these applications is able to play an alarm clock sound for more than 30 seconds when the alarm time is hit (usually the next morning).
I've tried two approaches already with no luck:
Approach 1:
[self performSelector:#selector(playAlarm) withObject:nil afterDelay:myDouble];
Approach 2:
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate =[datePicker date];//firedate;
notif.timeZone = [NSTimeZone systemTimeZone];
notif.alertBody = #"Time to wake up!";
NSString *SoundFileName=nil;
if([[[NSUserDefaults standardUserDefaults] objectForKey:#"ActualSoundFile"] isKindOfClass:[NSString class]])
SoundFileName=[[[NSString alloc]initWithString:[[NSUserDefaults standardUserDefaults]objectForKey:#"ActualSoundFile"]]autorelease];
else
SoundFileName=[[[NSString alloc] initWithString:#""] autorelease];
if([SoundFileName length]>1)
notif.soundName = [SoundFileName stringByAppendingString:#".wav"];
else
notif.soundName = UILocalNotificationDefaultSoundName;
notif.alertAction=#"Snooze";
notif.repeatCalendar=[NSCalendar currentCalendar];
notif.repeatInterval =NSDayCalendarUnit;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:#"Alarm" forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
Does anyone know how they're able to play the alarm on a loop after 7 hours?
The selected answer is not the right answer, because the user may wake up during the first notification and choose to close it. Guess what, the second notification comes along giving the user the impression that the alarm is broken.
The correct answer according to App docs is as follows:
You can not play a sound more than 30 seconds when your notification arrives while your app is in the background (e.g. user closes the app before going to sleep).
To play a longer sound, you must tell your user to leave the alarm app in the foreground before going to sleep, then in didReceiveLocalNotification you implement playing a longer sound manually.
You need to fire local notification by assigning date into fireDate property, and assign sound file into
UILocalNotification *localNotif = [[[UILocalNotification alloc] init]autorelease];
localNotif.fireDate = scheduleDate;
NSLog(#"fireDate is %#",localNotif.fireDate);
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"WAKE UP...!!!";
localNotif.alertAction = #"View";
localNotif.soundName = #"Default.wav";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
This way, local notification will be fired even if application is closed, remember that "Default.wav" file should be less than or equal to 30 seconds, Even Alarm clock pro app plays sound =30 seconds in local notification.
If application is alive, you can implement delegate method of appdelegate, and can apply your logic to display alert view and play sound even >30 seconds .....
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
}
So I think I found a valid solution:
To simulate the alarm sound playing for more than 30 seconds, just add multiple localnotifications one after the other, 30 seconds apart.