I am working on Alarm base project in iOS objective c.
I had implemented UILocalnotification for alarm when application is in background.
But problem i am facing is sound only plays for 30sec.
How can i increase the time interval for the sound or is there any other way to implement it instead of UILocalnotification .
- (IBAction)Alert:(id)sender{
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"yyyy-MM-dd"];
//NSDate *date = [NSDate date];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate =[NSDate dateWithTimeIntervalSinceNow:15];
localNotif.timeZone = [NSTimeZone localTimeZone];
localNotif.alertBody = #"Emergency";
localNotif.alertAction = #"View";
localNotif.soundName = #"police.mp3";
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval = NSYearCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Note : mp3 file i am using for UILocalnotification is of more that 60min duration.
Please advice.
Thank you
If device is in active mode, notification sound only play for 5 seconds. If device is in sleep mode, notification will play 30 seconds sound. I guess, this is standard behavi
As per Apple guidelines, you need to use mp3 file with 30 seconds duration. Please check below link.
https://developer.apple.com/reference/usernotifications/unnotificationsound
Note: FYI, UILocalNotification is deprecated from iOS10. You need to start using UNUserNotificationCenter.
Related
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];
link1
link2
If I want to set local notification to repeat every 2 week than how to achieve this?
I have read several questions and answers here, they have mention to reschedule local notification but how to reschedule local notification while application is in background ?
I will appreciate any help on same.
Thanks.
To reschedule UILocal Notification use this ,
NSDateFormatter *dat= [[NSDateFormatter alloc]init];
[dat setLocale:[NSLocale currentLocale]];
[dat setTimeZone:[NSTimeZone systemTimeZone]];
//[dat setDateFormat:#"YYYY-MM-dd"];// YYYY-MM-dd hh:mm a
//NSString *dateM=[dat stringFromDate:datM];
//[dat setDateFormat:#"YYYY-MM-dd h:mm a"];
NSDate *reminderDate=[NSDate date];
//Use manual option too for timing
reminderDate =[reminderDate dateByAddingTimeInterval:1*24*60*60*7];
UILocalNotification *localnoification = [[UILocalNotification alloc]init];
localnoification.fireDate=reminderDate;
localnoification.timeZone = [NSTimeZone defaultTimeZone];
localnoification.alertBody = word;
localnoification.alertAction = #"Tap to see word of the day";
//May Add Custom Sound Also
//localnoification.soundName = UILocalNotificationDefaultSoundName;
localnoification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
//localnoification.applicationIconBadgeNumber = 1;
localnoification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localnoification];
I have the code below in my AppDelegate.m applicationDidEnterBackground which will display a local notification 10 seconds after closing the app.
However, the time stamp (and any other data I want to include) is whatever it is at the time of creating the notification.
I would like to be able to show the latest data at the point of displaying the notification, as opposed to the data at the time of creating it.
Can I do this with the notification, or do I need to do something with background activity? And if so, what do I call / how in order to schedule that activity?
NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh-mm-ss"];
NSString *resultString = [dateFormatter stringFromDate: currentTime];
NSLog([NSString stringWithFormat:#"Time: %#",resultString]);
NSDate *alertTime = [[NSDate date]
dateByAddingTimeInterval:10];
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification* notifyAlarm = [[UILocalNotification alloc]
init];
if (notifyAlarm)
{
notifyAlarm.fireDate = alertTime;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval = 0;
notifyAlarm.alertBody = resultString;
[app scheduleLocalNotification:notifyAlarm];
}
AFAIK you are not able to show dynamic data in local notifications. If that dynamic data comes from server, then I'd suggest to use push notifications and build their messages with that dynamic data.
If your app is VoIP or Location-based app you can use corresponding background modes and then schedule local notification as soon as new data was polled.
Hello i am facing a weird kind problem. Actually i want to schedule a daily notification (only once a day) at 8:00 AM. Below is my code for scheduling daily notification.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm"];
NSDate *date = [[NSDate alloc] init];
date = [formatter dateFromString:#"08:00"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.alertBody = #"You just received a local notification";
localNotification.alertAction = #"View Details";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[formatter release];
[date release];
My problem is, I receive 2 local notifications. One at 8:00 AM and other at 10:00 AM. Why i am getting notification at 10:00 AM. I am scheduling it at 8:00 AM only. I know UILocalNotification library have some other weird kind of problems/bugs on most of apple devices. I just want to confirm whether there is some mistake in my code or it is a weird behaviour of UILocalNotification Library. I dont know why Apple is not working on resolving the issues being reported by many developers about UILocalNotification.
Note: I am using Xcode 4.6 and iOS 6.1
This would be the BEST link to get your answer.
I know this question has asked many times on StackOverflow but i couldn't able to set alarm in my app because i am very new to iOS? I am following this tutorial to set an alarm:
Setting a reminder using UILocalNotification in iOS.
However, it doesn't seems to be working for me.
I am in need to set alarm daily lets say 5.00 PM daily. I can't use date picker for choosing the time.
First on your xib, (or code) set the date picker mode: Time (Default is date & time)
The system assumes that the firedate is the current date, and the time is the time the user have chosen. This is not a problem because you set a repeat interval so it will work. I have tested it.
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
[localNotif setFireDate:datePicker.date];
[localNotif setRepeatInterval:NSDayCalendarUnit];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
PS: It would be a good idea to set the seconds to 0 using NSDateComponents class so as to set the alarm to ring at the first second of the minute you want. You can check the:
Local notifications in iOS.
tutorial you posted on how to do this.
+ (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID
Call this method with parameters and use this
+ (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
//set the notification date/time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:day];
[dateComps setMonth:month];
[dateComps setYear:year];
[dateComps setHour:hours];
[dateComps setMinute:minutes];
[dateComps setSecond:seconds];
NSDate *notificationDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = notificationDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Set notification message
localNotif.alertBody = alertBody;
// Title for the action button
localNotif.alertAction = actionButtonTitle;
localNotif.soundName = (alertSoundName == nil) ? UILocalNotificationDefaultSoundName : alertSoundName;
//use custom sound name or default one - look here to find out more: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html%23//apple_ref/doc/uid/TP40008194-CH103-SW13
localNotif.applicationIconBadgeNumber += 1; //increases the icon badge number
// Custom data - we're using them to identify the notification. comes in handy, in case we want to delete a specific one later
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
You may need to change the style of the date picker to allow changing the time in addition to just the date.
You can try this
UILocalNotification *todolistLocalNotification=[[UILocalNotification alloc]init];
[todolistLocalNotification setFireDate:[lodatepicker date]];
[todolistLocalNotification setAlertAction:#"Note list"];
[todolistLocalNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[todolistLocalNotification setAlertBody:text_todolist];
[todolistLocalNotification setHasAction:YES];