iOS Question Scheduling an Application

db0070

Active Member
Licensed User
Longtime User
How do I schedule an application to run after the application is in the background? I am trying to convert my B4A code in which I use a Service to start my application at a selected time.

I tried using local notification as below, but it does not start the application when the app is in the background

B4X:
Sub btnNotification (alarmTime As Long)
    Dim ln As Notification
    ln.Initialize(alarmTime) 'alarmTime seconds from now
    ln.IconBadgeNumber = 1
    ln.AlertBody = "Alarm Time"
    ln.PlaySound = True
    ln.Register
    hd.ToastMessageShow("Notification will fire in " & alarmTime & " seconds.", False)
End Sub

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

db0070

Active Member
Licensed User
Longtime User
At the scheduled time, I want to play the mp3 file that the user had selected, and at the same time give the user the opportunity to schedule another alarm with a different mp3 file. (Its the prayer time alarm application)
 
Upvote 0

db0070

Active Member
Licensed User
Longtime User
I have this code, and I run it on my iPhone in Release mode, at the schedule time I get the 'Notification arrived' message, but no sound - have I got my custom sound in the right place? My mp3 is less then 30 seconds and my mp3 is in Files\Special folder

B4X:
Private Sub Application_Start (Nav As NavigationController)
    Salat_Times.LocManager.Initialize("LocManager")
    NavControl = Nav
   
    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
           'did not work   
           'Dim no As NativeObject = ln  - did not work
           ' no.SetField("soundName", "adhan0.mp3")
            hd.ToastMessageShow("Application was started from a notification: " & ln.AlertBody, True)
        End If
    End If
    ge.Initialize("ge")
    Salat_Times.ShowSalat(False)
End Sub

Sub btnNotification (alarmTime As Long)
    Dim ln As Notification
    ln.Initialize(alarmTime) 'alarmTime seconds from now
   
    Dim no As NativeObject = ln
    no.SetField("soundName", "adhan0.mp3")

    ln.IconBadgeNumber = 1
    ln.AlertBody = "Adhan Alarm"
    ln.PlaySound = True
    ln.Register
    Log("Notification will fire in " & alarmTime & " seconds.")
End Sub

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

db0070

Active Member
Licensed User
Longtime User
In fact, the code does work .... I note that when the app is in the foreground the notification is fired but no sound is played. When I swipe app off, then the sound is played, and it is muted as soon as I open the app. Is that normal behaviour for an ios app?
 
Upvote 0
Top