Mind Pixel
Mind Pixel

Reputation: 814

Alarm is not ringing after 3 minutes when its running in background

I am working on a alarm application. I faced a problem from last 2 weeks. My problem is: My application is running in background.First time I install the application and set the alarm & close the app. If the alarm time is more than 3 minutes from current time then its not ringing means after 3 min alarm is not ringing in background process. If application is on then alarm is working.

This is my code:

   self->bgTask = 0;
   NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);

   bgTask = [application beginBackgroundTaskWithExpirationHandler: ^
   {
       NSLog(@"beginBackgroundTaskWithExpirat");
       dispatch_async(dispatch_get_main_queue(), ^
       {
           NSLog(@"dispatch_async");
           [application endBackgroundTask:self->bgTask];
           self->bgTask = UIBackgroundTaskInvalid;
       });
   }];

   dispatch_async(dispatch_get_main_queue(), ^
   {
       NSLog(@"dispatch_get_main_queue");
       //Start BG Timer
    [self Start_Update_Timer];
       self->bgTask = UIBackgroundTaskInvalid;
   });

// This is my timer for background running ..
 -(void) Start_Update_Timer
  {
        //If timer is already running, stop the timer
       if(self.callTimer)
       {
          [self.callTimer invalidate];
          self.callTimer=nil;
       }

   //call the timer for every one sec
   self.callTimer =[NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(update_Alarm_List) userInfo:nil repeats:YES];
      [[NSRunLoop mainRunLoop] addTimer:self.callTimer forMode:NSRunLoopCommonModes];
   }

Upvotes: 1

Views: 129

Answers (1)

Pochi
Pochi

Reputation: 13459

Because applications are not supposed to work like this. If you want your app to continuously play sound on the background, then it should be registered as an app that does it specially. Like a music player. If your app only delivers alarms, then you have to use a local notification to "trigger" the sound from your app. The notification can be played with a 30 secs sound and replay again at certain intervals. Your app wont play after 3 minutes because your "background" execution time has expired. That is the time that apple lets your app run in the background to do whatever you have to do after the user has closed your application.

This is for the case in where you want to keep playing sound on the background. Note that if the playback stops your app will stop.

https://developer.apple.com/library/ios/qa/qa1668/_index.html#//apple_ref/doc/uid/DTS40010209

If you want to use notifications with sound (30 secs max) see here:

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html

Upvotes: 2

Related Questions