0

I want to add a mute button to my app that when the user clicks it, he chooses a duration of the mute and while the mute is enabled the user won't get notifications from my app.

I thought NSTimer is a good option, but how can I make a timer with it? And how can I set that the timer will run in the background aswell?

Note: I'm using location monitoring in the background.

jscs
  • 63,694
  • 13
  • 151
  • 195
FS.O
  • 403
  • 6
  • 24

2 Answers2

1

I don't think you need a running timer in the background - or any complicated solution:

  • Use a timer when your app is in the foreground (as usual). Cancel the mute mode when it fires.

  • When your app switches to the background mode and the timer is active, cancel the timer or let it running, depending on your needs.

  • Optionally, when your app is executing in background mode, just don't send notifications.

  • If your app becomes suspended shortly after, your code wouldn't execute anyway.

  • If your app still needs to obey the mute state when it switches to the foreground, calculate the duration of the date-time value which you stored when the mute mode has been activated and the current date-time and compare this with your timeout.

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
  • My app sends notification based on the user's location, that's what it does so I need to send notification in the background. When the user want to mute for a certain time that app shouldn't send notification (for 5 hours for example), and after the certain time is over the app should start automatically resend notifications to the user. Any idea on how can I do this? – FS.O Feb 09 '16 at 08:26
  • @FS.O OK, I see. As long as your app is continuing running in the background mode, the timer should fire. Is this not happening? Note: when your app is _suspended_, then no code is executing anymore. – CouchDeveloper Feb 09 '16 at 08:31
  • It should work even if the user killed the app from the multitasking window, how can I do it? – FS.O Feb 09 '16 at 08:33
  • @FS.O if your app is _suspended_, all betts are off when it comes to the background mode again. Nonetheless, when your app becomes executing again it's easy to check whether it's in mute mode or not. Perhaps, you may need a way, where your app is waking up within an acceptable time span - through some external notification. See the duplicates - these may help. – CouchDeveloper Feb 09 '16 at 08:37
  • Where should I set the timer that it will call a method on my app after 5 minutes in any state the app is? – FS.O Feb 09 '16 at 08:40
  • @FS.O Immediately before your app will be suspended, setup an external notification. Take a look here, if you have not already: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW1 – CouchDeveloper Feb 09 '16 at 08:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102971/discussion-between-fs-o-and-couchdeveloper). – FS.O Feb 09 '16 at 08:44
-1

NSTimer can fire a method after a certain time interval

First declare a NSTimer object, say "timer"

self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(targetMethod:) userInfo:nil repeats:YES];

Now this timer will fire the targetMethod after every 1 sec. Time interval is 1 sec. Now define the target method

- (void) targetMethod: (NSTimer*)theTimer
{
    second++;

    if(second==60)
    {
        minute++;
        second=0;

    }
}

I declared two int variable for minute and second count. Now after every 1 sec the second will increase, and after every 60 sec, minute will increase. You can try it.

Muntaqeem Labbaeik
  • 331
  • 1
  • 3
  • 7