iOS Question background fetch

le_toubib

Active Member
Licensed User
Longtime User
hi all
i ' m using the following code :
#Region Project Attributes
#ApplicationLabel: Nosayba
#Version: 1.0.0
'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
#iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
#iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
#PlistExtra: <key>UIBackgroundModes</key><array><string>fetch</string></array>

#End Region

Private Sub Application_Start (Nav As NavigationController)
Page1.Initialize("Page1")
Page1.RootPanel.LoadLayout("Layout1")
Page2.Initialize("Page2")
Page2.RootPanel.LoadLayout("Layout2")
Page3.Initialize("Page3")
Page3.RootPanel.LoadLayout("Layout3")
PVC.Initialize("pvc", Array(Page1, Page2, Page3))
App.KeyController = PVC
App.RegisterUserNotifications(True, True, True) 'request permission for notifications
App.ApplicationIconBadgeNumber = 0

Dim no As NativeObject = App
no.RunMethod("setMinimumBackgroundFetchInterval:", Array(60))
End Sub

Private Sub Application_FetchDownload
'will run every 10 min to detect requests.php , and then jobdone and then notify and then complete
ReadOLDListOfMapsREQ
end sub

but the sub fetch_download is never reached ... what am i missing?
 

le_toubib

Active Member
Licensed User
Longtime User
i updated the above code to actually do something
#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, Page2, Page3, Page4 As Page
Private PVC As PageViewController
Private hd As HUD
Dim notify, CommingFromRequestNotification As Boolean
Dim Selective_Hospitals As List
Public ServerIP As String = "xxxx.com"

End Sub
Private Sub Application_Start (Nav As NavigationController)
Dim rl As ReleaseLogger
rl.Initialize("192.168.1.83", 54323)
#region pvc
Page1.Initialize("Page1")
Page1.RootPanel.LoadLayout("Layout1")
Page2.Initialize("Page2")
Page2.RootPanel.LoadLayout("Layout2")
Page3.Initialize("Page3")
Page3.RootPanel.LoadLayout("Layout3")
Page4.Initialize("Page4")
Page4.RootPanel.LoadLayout("Layout4")
Selective_Hospitals.Initialize
PVC.Initialize("pvc", Array(Page1, Page2, Page3, Page4))
App.KeyController = PVC
#end region

#region ''''''''''''''''notifi
notify = True
App.RegisterUserNotifications(True, True, True) 'request permission for notifications
App.ApplicationIconBadgeNumber = 0
'check whether the app was started from a notification

If App.LaunchOptions.IsInitialized Then
Dim ln As Notification = App.LaunchOptions.Get("UIApplicationLaunchOptionsLocalNotificationKey")
If ln.IsInitialized Then
hd.ToastMessageShow("Application was started from a notification: " & ln.AlertBody, True)
CommingFromRequestNotification=True
End If
End If
Dim no As NativeObject = App
no.RunMethod("setMinimumBackgroundFetchInterval:", Array(0))
Log("set minimum interval")
#end region


End Sub

Sub Application_ReceiveLocalNotification (LN As Notification)
'this event will fire if the scheduled notification happend when the app was running
Log("application received notifivation")
hd.ToastMessageShow("Notification arrived: " & LN.AlertBody, True)
End Sub


Private Sub Application_FetchDownload
'will run every 10 min to detect requests.php , and then jobdone and then notify and then complete
notify=True
' If Notify=True Then
' FetchingDownload
Log("application fetchdownload")
Dim getreq As HttpJob
getreq.Initialize("hostREQ",Me)
getreq.Download("http://" & ServerIP & "csv.php")
' ReadOLDListOfMapsREQ
'' End If
' OLDListOfAllRequests.Initialize
' OLDListPersonsFromService.Initialize
' If File.Exists(File.DirDocuments, "lperson.txt") Then
' Dim raf As RandomAccessFile
' raf.Initialize(File.DirDocuments, "lperson.txt" ,False)
' OLDListPersonsFromService= raf.ReadB4XObject(0) '(raf.CurrentPosition)
' raf.Close
' Dim raf As RandomAccessFile
' raf.Initialize(File.DirDocuments, "lallreq.txt" ,False)
' OLDListOfAllRequests = raf.ReadB4XObject(0) '(raf.CurrentPosition)
' raf.Close
' End If
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 getreq As HttpJob
' getreq.Initialize("hostREQ",Me)
' getreq.Download("")
'End Sub




Sub JobDone(Job As HttpJob)
If Job.Success Then
Dim res As String = Job.GetString2("UTF8") 'GetString
Log("Back from Job:" & Job.JobName )
Log("Response from server: " & res)
Select Job.JobName
Case "hostREQ"
Dim ln As Notification
ln.Initialize(DateTime.Now)
ln.AlertBody = res
ln.PlaySound = True
ln.Register
Dim out As OutputStream
out = File.OpenOutput(File.DirTemp,"req.csv",False )
File.Copy2(Job.GetInputStream, out)
out.Close
End Select
End If
Dim No As NativeObject = App
No = No.GetField("delegate")
No.RunMethod("completeFetch:", Array(0))
End Sub
but the application_FetchDownload event is still never reached. in both debug and release mode .. :(
i replaced the internal main.m file as instructed
 
Last edited:
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
i get this error from the ireleaselogger output once the screen go off
** -[UIApplication _handleNonLaunchSpecificActions:forScene:withTransitionContext:completion:] ** unhandled action -> <FBSSceneSnapshotAction: 0x15741750> {
handler = remote;
info = <BSSettings: 0x15644490> {
(1) = 5;
};
}
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
allright
after a while , i turned on the screen , only then i received the following msg from the logger :
application fetchdownload

response success
Back from Job:hostREQ
Response from server: ?
Timestamp date of admission Nature of Request Hospital Name Ward Bed Patient Name Age in years Gender Positive past psychiatric history Current Medical Diagnosis Reason for Consult Referring Dr Name Referring Dr Phone number Informed Doctor Inform time
Class (b4i_httpjob) instance released.
application received notifivation

which is the expected result
BUT
it only arrived when i turned the screen on , nothing even showed up on the logger while the screen was off ...

i stumbled upon this online , is it related ? or am i doing something wrong ?:
 
Last edited:
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
:( so it's actually foreground fetch ?
Ok, is there anyway to this in a better controlled way, I.e at least to "know" when the Godly System will decide download.
Is it still the same in case of push notifications. I.e only when the screen is on that the device wake - download - notify ?
- for logistic reasons I cannot do it with a push server , it's there any work around to do it without ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
so it's actually foreground fetch ?
No. If it was a foreground fetch then it only would have worked when your app is in the foreground.

Is it still the same in case of push notifications. I.e only when the screen is on that the device wake - download - notify ?
No. The message will arrive even if the device is sleeping.
it's there any work around to do it without ?
Probably no. Why do you need to download anything while the device is sleeping?
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
There is random (and frequent) updation of a Google spreadsheet , that I read a small part of it from a php file located online , so in the abscence of a push server , I have to download this php output every 10 minutes and check if it's changed , if yes -> notify promptly.
It's there a better way to do that without a push server active 24/7 ?
 
Upvote 0
Top