I have used the code from apples example from this page: Link, but I can't seem to get the sound to repeat. I have checked other applications, such as skype (for VOIP) and Alarm Clock Pro (audio?) but I cannot get the sound file to be repeated.
This is my code:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
AlarmHandler *AHinstance = getAlarmHandlerInstance();
UIApplication* app = [UIApplication sharedApplication];
NSArray *alarmList = [AHinstance getAlarms];
NSArray *oldNotifications = [app scheduledLocalNotifications];
if ([oldNotifications count] > 0)
{
[app cancelAllLocalNotifications];
}
for (Alarm *theAlarm in alarmList) {
NSDate *alarmDate = [theAlarm getNearestActivationDate];
Package *alarmPackage = [theAlarm getAlarmPackage];
NSArray *fileList = [alarmPackage getVoiceFileListForBackgroundNotificationWithHour:theAlarm.larmHour];
if( alarmDate == nil ) continue;
UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];
if (alarm)
{
NSLog(#"File: %#", [fileList objectAtIndex:0]);
alarm.fireDate = alarmDate;
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.soundName = [fileList objectAtIndex:0];
alarm.alertBody = #"Time to wake up!";
alarm.repeatInterval = 0;
[app scheduleLocalNotification:alarm];
}
}
}
Any suggestions on how I can fix this?
I have had suggestions to register app as audio player and play sounds in the background, but it seems that apple does take kindly to those applications because they aren't real audio players. Therefore they deny those apps.
Regards,
Paul Peelen
There is no way to do this for local notifications. You can either register as a VOIP app or as a "background audio" app, which have separate APIs. However, if you do not provide appropriate functionality to qualify for those kinds of uses, you'll most likely be rejected.
Yes this is possible, as the documentation states:
Your own applications can schedule up to 128 simultaneous notifications, any of which can be configured to repeat at a specified interval
You just need to configure the repeatInterval property:
The calendar interval at which to reschedule the notification.
Related
I have a VoIP application. Which is working fine. Call is working in foreground and background.
Following steps are done:
UIBackgroundModes => voip in Info.plist
Configured one of the app’s sockets for VoIP usage.
Before moving to the background, setKeepAliveTimeout:handler: is called
Configured audio session to handle transitions to and from active use.
To ensure a better user experience on iPhone, used the Core Telephony framework to adjust behavior in relation to cell-based phone calls;
To ensure good performance for VoIP app, used the System Configuration framework to detect network changes and allow app to sleep as much as possible.
Now the thing is when application is in background and a call comes then UILocalNotification fires for the following. And user can see a notification with two buttons CANCEL and RECEIVE
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask: bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_main_queue(), ^{
while ([application backgroundTimeRemaining] > 1.0) {
NSString *friend = [self checkForIncomingChat];
if ([friend length]>0) {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.alertBody = [NSString stringWithFormat: NSLocalizedString(#"%#", nil), friend];
localNotif.alertAction = NSLocalizedString(#"Receive", nil);
localNotif.soundName = #"alarmsound.caf";
localNotif.applicationIconBadgeNumber = 1;
[application presentLocalNotificationNow:localNotif];
friend = nil;
}
}
sleep(1);
}
[application endBackgroundTask:self->bgTask];
self->bgTask = UIBackgroundTaskInvalid;
});
}
- (NSString *) checkForIncomingChat {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *incall = [prefs objectForKey:#"incall"];
if ([incall length]>0) {
[prefs setObject:#"" forKey:#"incall"];
return incall;
}
return #"";
};
Now the problem is:
After going to the background by pressing home button application fires UILocalNotification if any call comes within 10 minutes.
After 10 minutes if any call comes it is running in the background. UILocalNotification does not fire, so user can not know anything.
It happens because background task stops after 10 minutes.
How can I manage it or extend background task for long running or restart background task.
More and more answer I have found after searching but nothing works for long running background task.
Please anybody help me. I am trying it since 2 weeks.
It sounds like you have a VoIP socket that you are receiving the call on, so rather than looping and polling for call state, you can just present the local notification at the point that you read the data off the socket.
If the VoIP control socket is TCP, and marked with the appropriate ...NetworkServiceTypeVoIP key, your app will automatically get woken up from suspension for 10 seconds, at which point you can present the local notification.
See Configuring Sockets for VoIP Usage for more information.
Once that is done, all of the code that you shared above can be removed.
I set the "application does not run in background" in my info.plist, so when user tap home button, app quits.
When my [UIApplication -appWillTerminate:] called, I will schedule 64 local notifications to system, all of them are non-repeating.
but that take a seemingly long time(6.17 seconds) on a iPhone4 with iOS6.0.1.
When I look at the time profiler, I found that the curve is very strange, it don't take much CPU time, but it do take a lot of time.
Also when I look at the call tree, 93% of the time is spent on [UIApplication -scheduleLocalNotification:] in the time range showed in the image.
Why?
This is how I generate my notifications:
UILocalNotification *n = [[[UILocalNotification] alloc] init] autorelease];
n.alertBody = #"some body";
n.hasAction = YES;
n.alertAction = #"some action";
n.fireDate = #"some date";
n.repeatInterval = 0;
n.soundName = #"my sound"
n.userInfo = aDictionaryWithAStringAbount10CharacterLongAnd2NSNumber.
[self.notifications addObject:n];
This is how I schedule my notifications:
-(void)endProxyAndWriteToSystemLocalNotification
{
_proxying = NO;
NSDate *dateAnchor = [NSDate date];
NSEnumerator *enumerator = [self.notifications objectEnumerator];
NSInteger i = 0;
while (i < maxLocalNotifCount) {
UILocalNotification *n = [enumerator nextObject];
if (!d) {
break;
}
if ([n.fireDate timeIntervalSinceDate:dateAnchor] >= 0) {
[[UIApplication sharedApplication] scheduleLocalNotification:n];
i++;
}
}
[self.notificationDatas removeAllObjects];
}
This would help:
-(void)endProxyAndWriteToSystemLocalNotification {
[[UIApplication sharedApplication] setScheduledLocalNotifications:self.notifications];
}
iOS 4.2 and later
read UIApplication Class Reference for detailed description
I think the problem is that you are trying to schedule 64 local notifications. Is there a reason to do all of these on app termination? Apples scheduleLocalNotification was not designed to be called so many times on termination
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.
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.