0

I am working on app where there is an MQTT connection with the server and server is sending some values related to device and UI changes accordingly. But when app is in background user should get local notification that certain values are changed. I know background service are not allowed in iOS but I want to make sure that is that there is no way to achieve this.

I successfully added local notification with app in background by UIApplication.shared.beginBackgroundTask but it's only work for 3 min exact after that apple terminates the app.

    func registerBackgroundTask() {
        backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
            self?.endBackgroundTask()
        }
        assert(backgroundTask != UIBackgroundTaskInvalid)
    }
    func endBackgroundTask() {
        print("Background task ended.")
        UIApplication.shared.endBackgroundTask(backgroundTask)
        backgroundTask = UIBackgroundTaskInvalid
    }

And just calling self.registerBackgroundTask() makes the app runnable in background for three min.

Next that I am going to try is that background fetch and widget to run service, Here I just want some suggestion that is there any chance that one of above two will work ?

Varun Naharia
  • 5,318
  • 10
  • 50
  • 84

2 Answers2

0

For more than 3 Minute. You will be enable any mode. Otherwise when app will enter in background app. After 3 min.App will not perform any action. enter image description here

Lalit kumar
  • 1,797
  • 1
  • 8
  • 14
  • but I am not using any location update then it's correct to do this ? – Varun Naharia Feb 27 '17 at 10:42
  • If you are not using any modes. In background app will not work after 3 min. Because Apple does not allow apps to continue to run in the background for more than a few minutes. – Lalit kumar Feb 27 '17 at 10:44
  • Yes I know that If i am not using any modes Apple does not allow apps to continue to run in the background but here I want to know is that is my situation comes under any mode that I can use ? – Varun Naharia Feb 27 '17 at 10:51
  • Yes it will resolve any mode .If you select location update than how to work in backgroundd. it may be good reference http://stackoverflow.com/questions/6347503/how-do-i-get-a-background-location-update-every-n-minutes-in-my-ios-application – Lalit kumar Feb 27 '17 at 11:00
0

It sounds like "Communicating with an External Accessory" would be the background mode that fits your application. See Apple Docs for reference. You have to activate Backround Mode for your project and set the value to "external-accessory". Then you can do ongoing small downloads in background. Apple mentions heart rate monitors as an example.

Please note that continous background polling is waste of energy and would deplete battery quickly. Check if this is really needed for your application. If the user just needs infrequent notifications/alarms, remote notifictions would be a much better solution. I use remote notifications in my own projects and it works very smooth and reliable. Additional benefit is, that it would wake up an app even if the user has closed it before.

transistor
  • 649
  • 6
  • 11
  • Actually there is socket connection in MQTT with the server and after connection is created server sends device status on some time interval and I don't know how to to this in background mode with any above mode that apple provides – Varun Naharia Feb 28 '17 at 15:43
  • 1
    External Accessories need to support the external accessory framework. See https://developer.apple.com/reference/externalaccessory. With a generic MQTT Device this is not possible. For using that device with iOS, you need to make it compatible to iOS, either through making it an "external accessory" or by using remote notifications. If you can't change the device, you need an own little server in between that will do the MQTT and translate it to iOS/background compatible traffic. – transistor Feb 28 '17 at 16:38
  • Thanks for the explanation I'll discuss about it with my team – Varun Naharia Feb 28 '17 at 17:02
  • @transistor you said "Remote notifications would wake up an app even if the user has closed it before.". Can you explain a little? Did you mean getting alerts when app is not running or another things? – Daedelus Mar 17 '17 at 06:33
  • This is of course one main functionality. You want to be notified at any time. Imagine your battery was empty and your phone was off. You won't want to start your email and twitter apps manually to get notifications again. See https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ – transistor Mar 18 '17 at 08:53
  • `that it would wake up an app even if the user has closed it before.` if your dialogue is true the answer this please https://stackoverflow.com/questions/49857236/trigger-local-notifications-automatically-daily-on-dynamic-time-given-in-arrays – iOS Developer Apr 20 '18 at 11:10