iOS Tutorial Background Fetch (Downloads)

Background fetch feature allows applications to run for a short period of time (up to 30 seconds) while in the background.

The steps required to use this service are:
1. Add the fetch mode declaration:
B4X:
#PlistExtra: <key>UIBackgroundModes</key><array><string>fetch</string></array>
2. Download the attached zip file and copy main.m to <project>\Files\Special.
3. Set the minimum interval (measured in seconds) with this code:
B4X:
Dim no As NativeObject = App
no.RunMethod("setMinimumBackgroundFetchInterval:", Array(0)) '0 = minimum interval

4. Handle the Application_FetchDownload event. It will be raised whenever your app is waked by this service:
B4X:
Private Sub Application_FetchDownload
   Log("FetchDownload")
   Dim ln As Notification
   ln.Initialize(DateTime.Now)
   ln.AlertBody = "Fetch download..."
   ln.PlaySound = True
   ln.Register
End Sub

5. When you completed the task you should run this code:
B4X:
Dim no As NativeObject = App
   no = no.GetField("delegate")
   no.RunMethod("completeFetch:", Array(0))
Note that you can run this code from any sub you like (JobDone for example).
The number (0 in the code above) is the UIBackgroundFetchResult. 0 means NewData.
The other values are listed here: https://developer.apple.com/library...ml#//apple_ref/c/tdef/UIBackgroundFetchResult

From my tests, with the minimum interval set to 0, the OS wakes the app every 10 - 15 minutes (when the screen is on).

B4X:
#Region  Project Attributes
   #ApplicationLabel: Background Fetch
   #Version: 1.0.0
   #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
   #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
   #PlistExtra: <key>UIBackgroundModes</key><array><string>fetch</string></array>
#End Region

Sub Process_Globals
   Public App As Application
   Public NavControl As NavigationController
   Private Page1 As Page

End Sub

Private Sub Application_Start (Nav As NavigationController)
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   NavControl.ShowPage(Page1)
   Dim no As NativeObject = App
   no.RunMethod("setMinimumBackgroundFetchInterval:", Array(0))
End Sub

Private Sub Application_FetchDownload
   Log("FetchDownload")
   Dim ln As Notification
   ln.Initialize(DateTime.Now)
   ln.AlertBody = "Fetch download..."
   ln.PlaySound = True
   ln.Register

   Dim no As NativeObject = App
   no = no.GetField("delegate")
   no.RunMethod("completeFetch:", Array(0))
End Sub
 

Attachments

  • main.zip
    516 bytes · Views: 1,205
Last edited:

fbritop

Active Member
Licensed User
Longtime User
If I have

#PlistExtra:<key>UIBackgroundModes</key><array><string>remote-notification</string></array>
and
#PlistExtra: <key>UIBackgroundModes</key><array><string>fetch</string></array>

How do we combine both main.h files you have provided for Silent Push and Background Fetch?

Thanks
FBP
 

fbritop

Active Member
Licensed User
Longtime User
Thanks
But my question was which "main.h" do I use, The one that came with silent notificacionts or the fetch one? Thay are not equaly the same file.

I need both features in my App.

Thanks
FBP
 

susu

Well-Known Member
Licensed User
Longtime User
How can I do Background Fetch Download for only registered users? Of course, I will check he is a registered user yet but if I add that codes my app will always fetch data, right?
 

fbritop

Active Member
Licensed User
Longtime User
It can be called anywhere in your applicaction. A global boolean variable should do the trick to check if you have made already the call to FetchInterval.

Ex:

B4X:
If fechtCall=False Then
    If userIsRegistered=True Then
        fechtCall=True
        Dim no As NativeObject = App
       no.RunMethod("setMinimumBackgroundFetchInterval:", Array(0))
    End If
End If
 

susu

Well-Known Member
Licensed User
Longtime User
Oh thank you. I thought this code to set the minimum interval like Erel wrote:

3. Set the minimum interval (measured in seconds) with this code...
 

fbritop

Active Member
Licensed User
Longtime User
That's correct. The minimum you can register is 30 seconds according to iOS developer site
 

narek adonts

Well-Known Member
Licensed User
Longtime User
What I understood here is that there is possibility to add 3rd party .m files with adding them to special folder. Like the main.m file.
So we are not forces to create a library, when can copy the .m file and use it with NativeObject?
Right ?
 

AHilton

Active Member
Licensed User
Longtime User
Does the Simulator not support Background Fetch or am I missing something ?

I get this error ...

Method not found: setMinimumBackgroundFetchInterval:, target: [IsInitialized=0, DeviceUID=(null), CompanyUID=(null)

On this code line ...

no.RunMethod("setMinimumBackgroundFetchInterval:", Array(0)) '0 = minimum interval
 

AHilton

Active Member
Licensed User
Longtime User
Bingo! That was it, narek adonts.

"no" was set to NativeObject = App but it needed to be TheApp, instead. App, in this project, points to another variable.

Thanks.
 

Luiz Fernando Orlandini

Active Member
Licensed User
Longtime User
This is awesome. Quick question about remote notifications...

Is it possible to run this code when a Push is received? This means that application:didReceiveRemoteNotification:fetchCompletionHandler will be handled by B4I?

Also, how can I get the message sent via push to take my decisions into APP code and also download any pertinent data?

PS.: I couldn't be able to find Application_FetchDownload documented at all into core.
 

electro179

Active Member
Licensed User
Longtime User
Hello

When I use the Background Fetch

The function that called is only Application_FetchDownload ?
What is the period ?

It is configured with

no.RunMethod("setMinimumBackgroundFetchInterval:", Array(0))
how much second for 0

if I set 5, is it 5 seconds ?


Can I received the event on the UDPSocket_PacketArrived (packet As UDPPacket) ?

Thank you
 
Top