'Code module
#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
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public App As Application
Public NavControl As NavigationController
Private Page1 As Page
Dim RL As ReleaseLogger
End Sub
Sub Application_Start (Nav As NavigationController)
RL.Initialize("172.30.4.87", 54323) 'load inetwork library
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))
Log("Starter")
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
All right!!HI,
In the simulator, works application_fetchdownload?
I copy the example, but, not work, never handle the application_fechdownload event.... I replace the main.h from "Program Files\Anywhere Software\B4i\Project"..
I can not be doing wrong
my code:
can anybody help me...please!!!!B4X:'Code module #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 'These global variables will be declared once when the application starts. 'Public variables can be accessed from all modules. Public App As Application Public NavControl As NavigationController Private Page1 As Page Dim RL As ReleaseLogger End Sub Sub Application_Start (Nav As NavigationController) RL.Initialize("172.30.4.87", 54323) 'load inetwork library 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)) Log("Starter") 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
Thank's
#PlistExtra: <key>UIBackgroundModes</key><array><string>fetch</string></array>
Nav.NavigationBarVisible=False
Nav.ToolBarVisible=False
App.RegisterUserNotifications(True, True, True) 'request permission for notifications
App.ApplicationIconBadgeNumber = 0
GlobalVars.NavControl = Nav
Dim no As NativeObject = App
no.RunMethod("setMinimumBackgroundFetchInterval:", Array(5)) '5 seconds
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
This is not needed if you are using v2.50. You should put it under Files\Special.Also i changed the main.m file on my B4i installation folder as @Erel told on another post.
This is exactly how the background fetch mode is expected to work.Sometimes it raises after 10 minutes, others after 13 minutes, others 16, does not follows a pattern.
Sounds to me that you need to use push notifications instead.Is it a way to make a task run background with a reasonable pattern?
I readed later your post with the results of setting the minimum to 0.Erel said:This is exactly how the background fetch mode is expected to work.
I suspected it.. Thanks a lot.Erel said:Sounds to me that you need to use push notifications instead.
Code module
#Region Project Attributes
#ApplicationLabel: Notifiche BackGround
'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
#iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
#iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
#Target: iPhone, iPad
#MinVersion: 7
#PlistExtra: <key>UIBackgroundModes</key><array><string>fetch</string></array>
#End Region
'Per Sviluppo non distribuzione
#CertificateFile: ios_development.cer
#ProvisionFile: Ultimo.mobileprovision
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public App As Application
Public NavControl As NavigationController
Private Page1 As Page
Dim ln As Notification
Private Button1 As Button
Private WebView1 As WebView
End Sub
Private Sub Application_Start (Nav As NavigationController)
NavControl = Nav
Page1.Initialize("Page1")
Page1.Title = "Page 1"
Page1.RootPanel.Color = Colors.White
Page1.RootPanel.LoadLayout("main")
NavControl.ShowPage(Page1)
App.RegisterUserNotifications(True,True,True)
App.ApplicationIconBadgeNumber=0
Dim no As NativeObject = App
no.RunMethod("setMinimumBackgroundFetchInterval:", Array(35)) 'minimunm >30 seconds (PS: APP in BACKGROUND !)
End Sub
Private Sub Page1_Resize(Width As Int, Height As Int)
End Sub
Private Sub Application_Background
Log("Sono in BackGround")
End Sub
Private Sub Application_FetchDownload
App.ApplicationIconBadgeNumber=33
'Log ("parte " & DateTime.Now)
Dim Job1 As HttpJob
Job1.Initialize("dealoftheday", Me)
Job1.Download("http://www.iusondemand.it/exvoto/home.txt")
End Sub
Sub JobDone (Job As HttpJob)
'Log ("done " & DateTime.Now)
If Job.Success = True Then
Select Job.JobName
Case "dealoftheday"
If Job.GetString.Contains("a") Then
If ln.IsInitialized = False Then
ln.Initialize(DateTime.Now)
ln.AlertBody = Job.GetString & DateTime.Now
ln.PlaySound = True
ln.IconBadgeNumber = 1
ln.Register
App.ApplicationIconBadgeNumber=2
Dim no As NativeObject = App
no = no.GetField("delegate")
no.RunMethod("completeFetch:", Array(0))
End If
End If
End Select
End If
Job.Release
End Sub
Private Sub Application_Foreground
'Log ("azzera " & DateTime.Now)
App.ApplicationIconBadgeNumber=0
End Sub
Sub Button1_Click
WebView1.LoadHtml("test " & DateTime.Now)
End Sub
It works, after restarting the deviceNote that you need to call completeFetch in all cases. You can simplify the code with a single resumable sub.
Set the minimum interval to 0.
Don't kill the app with a swipe.
Leave the phone connected to the charger.
The expected result is that Application_FetchDownloads runs once in a while.
'Code module
#Region Project Attributes
#ApplicationLabel: B4i Background
#Version: 1.0.0
'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
#iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
#iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
#Target: iPhone, iPad
#ATSEnabled: True
#MinVersion: 7
#End Region
'Link --> https://www.b4x.com/android/forum/threads/background-fetch-downloads.56022/#content
'Per Sviluppo non distribuzione
#CertificateFile: ios_development.cer
#ProvisionFile: Ultimo.mobileprovision
#PlistExtra: <key>UIBackgroundModes</key><array><string>fetch</string></array>
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public App As Application
Public NavControl As NavigationController
Private Page1 As Page
End Sub
Private Sub Application_Start (Nav As NavigationController)
'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
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)) '0 = minimum interval
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
Private Sub Page1_Resize(Width As Int, Height As Int)
End Sub
Private Sub Application_Background
End Sub
'Code module
#Region Project Attributes
#ApplicationLabel: B4i Background
#Version: 1.0.0
'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
#iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
#iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
#Target: iPhone, iPad
#ATSEnabled: True
#MinVersion: 7
#End Region
'Link --> https://www.b4x.com/android/forum/threads/background-fetch-downloads.56022/#content
'Per Sviluppo non distribuzione
#CertificateFile: ios_development.cer
#ProvisionFile: Ultimo.mobileprovision
#PlistExtra: <key>UIBackgroundModes</key><array><string>fetch</string></array>
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public App As Application
Public NavControl As NavigationController
Private Page1 As Page
Private hd As HUD
End Sub
Private Sub Application_Start (Nav As NavigationController)
'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
NavControl = Nav
Page1.Initialize("Page1")
Page1.Title = "Page 1"
Page1.RootPanel.Color = Colors.White
NavControl.ShowPage(Page1)
'For Notification
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)
Log("Application was started from a notification")
End If
End If
Dim no As NativeObject = App
no.RunMethod("setMinimumBackgroundFetchInterval:", Array(0)) '0 = minimum interval
End Sub
Private Sub Application_FetchDownload
Log("FetchDownload")
Dim ln As Notification
ln.Initialize(DateTime.Now)
ln.IconBadgeNumber = 1
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
Private Sub Page1_Resize(Width As Int, Height As Int)
End Sub
Private Sub Application_Background
Log("sono in background")
End Sub
Private Sub Application_Foreground
App.ApplicationIconBadgeNumber=0
End Sub
Sub Application_ReceiveLocalNotification (ln As Notification)
'this event will fire if the scheduled notification happend when the app was running
hd.ToastMessageShow("NA: " & ln.AlertBody, True)
End Sub
Any solution ?Can't endBackgroundTask: no background task exists with identifier 403, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
Yes @Erel i understood everything and i followed your instructions step by step.You must test it in release mode.
FetchDownload should eventually be called. However there is no guarantee when will it happen and in which frequency.
One time. only one time. So dont work.You wrote in post #32 that it worked. What has changed?