Android Question How to automatically show messages from app on detecting internet connectivity

Makumbi

Well-Known Member
Licensed User
Iam currently using firebase to send notification to all users . but sometimes you find that some users phone may either be off or due to internet challenges my not receive notification at that time . When i send notification i save a copy into my database .so is their a way of automatically bring back the sent messages immediately when ever there is internet connectivity even when the user has not opened the app. as it is with Whatsapp. because as soon as you connect the internet the messages automatically comes thanks in advance.

The code i have here returns the messages only when some has opened the app . But i wanted these messages to automatically come as soon as there is internet connectivity. because what happens with firebase notification they only come when the phone is currently on at that time . if the phone is switch on the second day they don't come . So i wanted as soon as internet is detected then the person who didnt get a message can automatically be notified when he has not opened the app

B4X:
Sub Service_Start (StartingIntent As Intent)
    cursor = SQL1.ExecQuery("SELECT phone AS Phone,Sex FROM Register")
    If cursor.RowCount > 0 Then
        cursor.Position =0
        Dim Phonet As String
        Phonet = cursor.Getstring("Phone")
        Dim sx As String
        sx = cursor.Getstring("Sex")
        '    sx = "Father"
    End If
    Dim CustID As String = Phonet' Customer ID
    Dim jtk As HttpJob
    jtk.Initialize("", Me)
    'j.Download("http://192.168.1.239/Generic_Handler_JSON/HandlerVB.ashx?customerid=" & CustID)
    jtk.Download("http://kccug.com/Generic_Handler_JSON/HandlerVBGetreply.ashx?customerid=" & CustID & "&bcode=" & sx)
    'jtk.Download("http://192.168.1.239/Generic_Handler_JSON/HandlerVBGetreply.ashx?customerid=" & CustID & "&bcode=" & sx)
    'jtk.GetRequest.Timeout = 10000 ' 10 seconds
    Wait For (jtk) JobDone(jtk As HttpJob)

    If jtk.Success Then ' if job is success (http status code 200)
        Dim RetVal As String
        Log(RetVal)
        RetVal = jtk.GetString
        If jtk.GetString = "[]" Then
            'Msgbox("Please try Again ","SMIS")
            'Return
            
        Else
                    
            Dim jpt As JSONParser
            jpt.Initialize(jtk.GetString)
            'Log(jpt) ' will pr
            Log($"Token(${jpt})"$)
            Dim quotes As List = jpt.NextArray
            For Each quot As Map In quotes
                Log("Account: " & quot.Get("Account"))
                Log("sms: " & quot.Get("sms"))
                Log("Datesent: " & quot.Get("Datesent"))
                
                Service.StartForeground(nid, CreateNotification(quot.Get("sms")))
                StartServiceAt(Me, DateTime.Now + 60 * DateTime.TicksPerMinute, True)
          
            Next
            
        End If
    
    End If
    
End Sub
 
Last edited:

JohnC

Expert
Licensed User
Longtime User
Off the top of my head, I don't know of any Intent that gets broadcast when the device "connects" to the internet. If there is one, then maybe you can setup a static intent (or maybe a dynamic intent if you run a continuous service) to handle it.

My only other suggestion is to create a service that you schedule to run every 5 or 10 minutes (try Erel's background location tracking sample for an example of how to do this) and have it keep trying to check your database and see if there were any new messages sent during the time the device was not connected to the internet. If the device still doesn't have an internet connection, then it will check again in 5-10 minutes, and so on until it gets a connection and if there are any new messages, notify the user.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…