0

Problem:

I want to run specific code at specific clock time even if the application is background state.

Approach Tried:

  1. I tried using NSTimer but the NSTimer pauses when the application enters the background state.
  2. I am not using the "Background Fetch" approach, as there will be long time before I want the code to execute.

I have seen an third party iOS alarm application which starts playing music when alarm goes off. Per my research, there is no way you can schedule a task in iOS (like Android). I want to know the workaround solution to implement such functionality.

I would appreciate any suggestions and thoughts on this topic. Thank you.

rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

1

Only Way -- SILENT REMOTE NOTIFICATIONS

This will awake your app in the background and you can catch the notification in application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) method.

Something like this...

if launchOptions != nil,
        let userInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable : Any] {
        // put your method here..
    }

More on silent notification -- Silent Remote Notification

Shrey
  • 1,959
  • 2
  • 21
  • 44
  • Thank you for the reply. Will the application receive this callback if the notifications are disabled? – Nimesh Chandramaniya May 26 '19 at 05:27
  • 1
    Nope... notifications should be enabled. You need user's permission – Shrey May 26 '19 at 05:29
  • Interesting. The third party application I mentioned above, able to play music when alarm is triggered even if the notifications are disabled. Any other thoughts how this app is able to play music in background? – Nimesh Chandramaniya May 26 '19 at 05:34
  • Can you share the app URL you are referring too? – Shrey May 27 '19 at 12:10
  • Link: https://itunes.apple.com/us/app/alarmy-alarm-clock/id1163786766 – Nimesh Chandramaniya May 27 '19 at 13:10
  • I installed the app and it clearly says "Will need you to allow notifications to wake you up in time". App is not allowing to move forward until i give permission for the notifications. Looks like they are indeed using silent notifications to get the job done. – Shrey May 28 '19 at 06:42
  • Thank you for the reply. Try disabling the notifications and put you device on airplane mode. The application is still able to function properly. – Nimesh Chandramaniya May 28 '19 at 12:57
  • I tried it, the app will function only if it's in foreground. But when you kill the app with airplane mode on and notifications disabled, app will not fire any alarms. Looks like they are using local and push notifications with NSTimer for foreground. – Shrey May 29 '19 at 04:37
  • Sorry for the delay in the reply. I agree with your observation that if we **kill** the app, the alarms are not fired. But if you put the app in the background state by pressing the home button, alarms are triggered (even if the device is on airplane mode and notifications are disabled). Per my understanding, **NSTimer** can only run for 3 minutes if the app is in background state. So, there is still a mystery about how this app is able to trigger an alarm. – Nimesh Chandramaniya Jun 10 '19 at 11:45
1

You can do it with silent push notification but still, you can not be sure that the user will receive the push notification. Another option is the schedule local notification but there is no way to execute code from local notification if the user does not click it or application is not opened. There is no way to guarantee that the code will be executed in the exact time. Your question sounds like this one

m1sh0
  • 2,236
  • 1
  • 16
  • 21