iOS Question Some questions about Background Fetch

susu

Well-Known Member
Licensed User
Longtime User
Hi,

My app used Background Fetch to get "Deal of the Day" info from my server. If there's a deal, the server will output like: "Deal of the Day: Buy 1 beer get 1 coke free". If there's no deal, the output like: "No DOTD".

Code of my app is:
B4X:
'Code module

Sub Process_Globals
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Dim ln As Notification
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(5)) '5 seconds
End Sub

Private Sub Application_FetchDownload
   Dim Job1 As HttpJob
   Job1.Initialize("dealoftheday", Me)
   Job1.Download("http://mywebsite.com/dealoftheday.php")
End Sub

Sub JobDone (Job As HttpJob)
    If Job.Success = True Then
        Select Job.JobName
            Case "dealoftheday"
                If Job.GetString.Contains("Deal of the Day:") Then
                    If ln.IsInitialized = False Then
                        ln.Initialize(DateTime.Now)
                        ln.AlertBody = Job.GetString
                        ln.PlaySound = True
                        ln.IconBadgeNumber = 1
                        ln.Register
                   
                        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
    If ln.IsInitialized Then
        ln.IconBadgeNumber = 0
        'ln.Cancel  still no luck
    End If
End Sub

My questions:
1. Can I insert Background Fetch code in any module or I need to put it in Main module?

2. Do I need to insert Background Fetch code in Application_Start or I can put it anywhere?

3. How to remove Icon Badge Number when user open my app? I tried ln.IconBadgeNumber = 0 and ln.Cancel but icon badge number still there.

4. In code, I set the interval is 5 seconds. In my test, the device connected to internet with the screen off. I received notification one by one after 1 hour, can I do something to receive notification to be more precise?

5. Where's the reference for Notification? I can't find it in any document.

Thank you.
 
Top