B4J Question WS_Eval issue

Harris

Expert
Licensed User
Longtime User
I have the B4X PushServer running (finally).

I am trying to send a simple message from my ABM application using ws.Eval().

This:
Dim script As String = "//104.168.173.164:51046/send?password=123&text="&msg

ws.Eval("window.location = arguments[0]", Array As Object(script))
replaces the current page with a new page (browser window) and sends the message to the server and delivers it to the Android device
(http://104.168.173.164:51046/send?password=123&text=my new message)


This:
' ws.Eval(script, Null)
does nothing....

What do I need to do here?

Thanks

B4X:
Sub Sendmessage( msg As String)

Dim script As String = "//104.168.173.164:51046/send?password=123&text="&msg   
'Dim script As String = $"//localhost:51046/send?password=123&text=${msg}"$

  ws.Eval("window.location = arguments[0]", Array As Object(script))

' ws.Eval(script, Null)
   
End Sub
 

Harris

Expert
Licensed User
Longtime User
Included the PushServer into my app. Using GCM (FCM) and required codes... (legacy)

Since I already have a server running, why run another?

Works great until FCM, GCM does't push message out.


return result...

<HTML>
<HEAD>
<TITLE>The request was missing an Authentication Key (FCM Token). Please, refer to section &quot;Authentication&quot; of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>The request was missing an Authentication Key (FCM Token). Please, refer to section &quot;Authentication&quot; of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

Error sending Android message: The request was missing an Authentication Key (FCM Token). Please, refer to section "Authentication" of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.


Wait 30 secs - 2 minutes and next message will go.

WT heck...????

B4X:
Public Sub SendMessageTo(Devices As List, msg As Message, usid As String)
   
    Dim json As JSONGenerator
    Dim m As Map
    m.Initialize
    m.Put("registration_ids", Devices)
    Dim data As Map
    data.Initialize
    data.Put("data", msg.Text)
   
    data.Put("tsent", DateTime.Now)
    data.Put("sentby", usid)
   
    'you can add more fields to data and then read them on the device.
    m.Put("data", data)

    json.Initialize( m)
    Dim Job As HttpJob
    Job.Initialize("send message", Me)

'    Job.PostString("https://android.googleapis.com/gcm/send", json.ToString)
' trying fcm ...

    Job.PostString("https://fcm.googleapis.com/fcm/send", json.ToString)
    Job.GetRequest.SetContentType("application/json")
    Job.GetRequest.SetHeader("Authorization", "key=" & Main.AndroidApiKey)
   

    Log(" My API key: "&Main.AndroidApiKey)
   
End Sub
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
I don't understand. Does it sometimes work and sometimes fail?
Yes, sometimes fails - on my server... Works everytime from my desktop development system...

AndroidPush code:
In the JobDone, if error is detected, I try again. Will try up to 3 times.
I have seen it make 2 attempts before message was sent.


B4X:
Sub Process_Globals
    Private Const DAYS_LIMIT As Int = 300
'    Type Message(Text As String, Sound As Boolean, Badge As Int, ExpirationDate As Long, HighPriority As Boolean)
    Private npass, nmess, nusid As String
    Private mctr As Int = 0
   
End Sub


'Sub Handlemessage(req As ServletRequest, resp As ServletResponse)
Sub Handlemessage(pass As String, mess As String, usid As String)
   
    npass = pass
    nmess = mess
    nusid = usid
   
    If pass <> Main.PushServerPassword Then
        'resp.SendError(500, "Invalid password")
        Log(" wrong password")
    '    Return
    End If
    Dim m As Message
    m.Initialize
'    If IsNumber(req.GetParameter("badge")) Then
'        m.Badge =  DateTime.Now 'req.GetParameter("badge")
'    End If
    m.Text = mess ' req.GetParameter("text")
    Log(" send text: "&m.Text)
   
    m.Sound = True
    m.HighPriority = True
    m.ExpirationDate = DateTime.Now + DateTime.TicksPerDay
    Dim SQL As SQL = DBM.GetSQL


    Dim rows As List = DBUtils.ExecuteMemoryTable(SQL, _
        "SELECT * FROM tokens WHERE updated > ? ORDER BY updated DESC", _
        Array As String (DateTime.Now - DAYS_LIMIT * DateTime.TicksPerDay), 0)

    'Dim rows As List = DBM.SQLSelect(SQL,  "SELECT * FROM tokens",Null)
'    ") ' WHERE updated > ? ORDER BY updated DESC", Array As Long (DateTime.Now - (DAYS_LIMIT * DateTime.TicksPerDay)))
    Log("SELECT * FROM tokens WHERE updated ")
    DBM.CloseSQL(SQL)
   
'    Dim rows As List = DBUtils.ExecuteMemoryTable(Main.db, _
'        "SELECT token, type FROM tokens WHERE updated_time > ? ORDER BY updated_time DESC", _
'        Array As String (DateTime.Now - DAYS_LIMIT * DateTime.TicksPerDay), 0)
       
       
    Dim iosTokens, AndroidTokens As List
    iosTokens.Initialize
    AndroidTokens.Initialize
    For Each row() As String In rows
        If row(1) = Main.TYPE_IOS Then
            iosTokens.Add(row(1))
            If iosTokens.Size > 900 Then
'                CallSubDelayed3(iOSPush, "SendMessageTo", iosTokens, m)
                Dim iosTokens As List
                iosTokens.Initialize
            End If
        Else
            AndroidTokens.Add(row(1))
            Log(" Row item: "&row(1))
            If AndroidTokens.Size > 900 Then
                Log(" sending message to: "&AndroidTokens.Get(0))
                SendMessageTo(AndroidTokens, m, usid)
                Dim AndroidTokens As List
                AndroidTokens.Initialize
            Else
                Log(" token too small - no send here.. "&AndroidTokens.Size)   
            End If
        End If
    Next
'    If iosTokens.Size > 0 Then CallSubDelayed3(iOSPush, "SendMessageTo", iosTokens, m)
    If AndroidTokens.Size > 0 Then
        Log(" Sending here - token size: "&AndroidTokens.Size)
'         CallSubDelayed3(Me, "SendMessageTo", AndroidTokens, m)
        SendMessageTo(AndroidTokens, m, usid)
        
    End If
   
'    resp.Write("Message sent to " & rows.Size & " device(s).")

End Sub


Public Sub SendMessageTo(Devices As List, msg As Message, usid As String)
   
    Dim json As JSONGenerator
    Dim m As Map
    m.Initialize
    m.Put("registration_ids", Devices)
    Dim data As Map
    data.Initialize
    data.Put("data", msg.Text)
   
    Dim tim As Map
    tim.Initialize
    data.Put("tsent", DateTime.Now)
   
   
    Dim tim1 As Map
    tim1.Initialize
    data.Put("sentby", usid)
   
    'you can add more fields to data and then read them on the device.
    m.Put("data", data)

    json.Initialize( m)
    Dim Job As HttpJob
    Job.Initialize("send message", Me)

'    Job.PostString("https://android.googleapis.com/gcm/send", json.ToString)


    Job.PostString("https://fcm.googleapis.com/fcm/send", json.ToString)
    Job.GetRequest.SetContentType("application/json")
    Job.GetRequest.SetHeader( "Authorization", "key=" & Main.AndroidApiKey)
   
'    Job.GetRequest.SetContentType("application/json")
    'Job.GetRequest.SetHeader("Authorization", "key=" & Main.AndroidApiKey)

    Log(" My API key: "&Main.AndroidApiKey)
   
End Sub



Private Sub JobDone(j As HttpJob)
    If j.Success Then
        Log("Message was sent successfully")
        npass = ""
        nmess = ""
        nusid = ""
        mctr = 0
        j.Release

    Else
        Log("Error sending Android message: " & j.ErrorMessage)
        j.Release

        mctr = mctr + 1
        If mctr < 3 Then
            Log(" Attempting to send after error #: "&mctr)
           Handlemessage(npass, nmess, nusid)       
        End If  
       
    End If
End Sub


Server Logs:

Message 1:
send text: send this out
Row item: APA91bGBeQfIgbO_Z-bi0_adZidwMoEfV5JsenLMZnqmLVjT2zfKwGIl7l2zEjhUkFfwGq7UZ7A9XiZoK2P3wsFj1O72TAv7Tq_Xb-IxIxsiaRcYW5f8n_A
Row item: APA91bEfXcO-0X0CwVQxIWr29KZFt-hrEeqRY3026cHCc__dpwXLUAbuSn66dpv_FRsV5KSFmldAL4y4_szb_TbTOZdS00it3qYMqv-p-E7z3OXfzOE6UVI

Sending here - token size: 2
My API key: AIzaSyDveofcE52FAIVg5ZCuC9VpiRwNLNIVAJw

Message was sent successfully



Message 2:
send text: now this one
Row item: APA91bGBeQfIgbO_Z-bi0_adZidwMoEfV5JsenLMZnqmLVjT2zfKwGIl7l2zEjhUkFfwGq7UZ7A9XiZoK2P3wsFj1O72TAv7Tq_Xb-IxIxsiaRcYW5f8n_A
Row item: APA91bEfXcO-0X0CwVQxIWr29KZFt-hrEeqRY3026cHCc__dpwXLUAbuSn66dpv_FRsV5KSFmldAL4y4_szb_TbTOZdS00it3qYMqv-p-E7z3OXfzOE6UVI
Sending here - token size: 2
My API key: AIzaSyDveofcE52FAIVg5ZCuC9VpiRwNLNIVAJw
<HTML>
<HEAD>
<TITLE>The request was missing an Authentication Key (FCM Token). Please, refer to section &quot;Authentication&quot; of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>The request was missing an Authentication Key (FCM Token). Please, refer to section &quot;Authentication&quot; of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

Error sending Android message: The request was missing an Authentication Key (FCM Token). Please, refer to section "Authentication" of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.
Attempting to send after error #: 1 (try upto 3 times...)

Second Attempt of Message 2 - after detecting error in JobDone()
send text: now this one
Row item: APA91bGBeQfIgbO_Z-bi0_adZidwMoEfV5JsenLMZnqmLVjT2zfKwGIl7l2zEjhUkFfwGq7UZ7A9XiZoK2P3wsFj1O72TAv7Tq_Xb-IxIxsiaRcYW5f8n_A
Row item: APA91bEfXcO-0X0CwVQxIWr29KZFt-hrEeqRY3026cHCc__dpwXLUAbuSn66dpv_FRsV5KSFmldAL4y4_szb_TbTOZdS00it3qYMqv-p-E7z3OXfzOE6UVI
Sending here - token size: 2
My API key: AIzaSyDveofcE52FAIVg5ZCuC9VpiRwNLNIVAJw
Message was sent successfully

Message 3:
send text: and this one
Row item: APA91bGBeQfIgbO_Z-bi0_adZidwMoEfV5JsenLMZnqmLVjT2zfKwGIl7l2zEjhUkFfwGq7UZ7A9XiZoK2P3wsFj1O72TAv7Tq_Xb-IxIxsiaRcYW5f8n_A
Row item: APA91bEfXcO-0X0CwVQxIWr29KZFt-hrEeqRY3026cHCc__dpwXLUAbuSn66dpv_FRsV5KSFmldAL4y4_szb_TbTOZdS00it3qYMqv-p-E7z3OXfzOE6UVI
Sending here - token size: 2
My API key: AIzaSyDveofcE52FAIVg5ZCuC9VpiRwNLNIVAJw
<HTML>
<HEAD>
<TITLE>The request was missing an Authentication Key (FCM Token). Please, refer to section &quot;Authentication&quot; of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>The request was missing an Authentication Key (FCM Token). Please, refer to section &quot;Authentication&quot; of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

Error sending Android message: The request was missing an Authentication Key (FCM Token). Please, refer to section "Authentication" of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.
Attempting to send after error #: 1

Second Attempt of Message 3 - after detecting error in JobDone()

send text: and this one
Row item: APA91bGBeQfIgbO_Z-bi0_adZidwMoEfV5JsenLMZnqmLVjT2zfKwGIl7l2zEjhUkFfwGq7UZ7A9XiZoK2P3wsFj1O72TAv7Tq_Xb-IxIxsiaRcYW5f8n_A
Row item: APA91bEfXcO-0X0CwVQxIWr29KZFt-hrEeqRY3026cHCc__dpwXLUAbuSn66dpv_FRsV5KSFmldAL4y4_szb_TbTOZdS00it3qYMqv-p-E7z3OXfzOE6UVI
Sending here - token size: 2
My API key: AIzaSyDveofcE52FAIVg5ZCuC9VpiRwNLNIVAJw
Message was sent successfully


Message 4:
send text: now this
Row item: APA91bGBeQfIgbO_Z-bi0_adZidwMoEfV5JsenLMZnqmLVjT2zfKwGIl7l2zEjhUkFfwGq7UZ7A9XiZoK2P3wsFj1O72TAv7Tq_Xb-IxIxsiaRcYW5f8n_A
Row item: APA91bEfXcO-0X0CwVQxIWr29KZFt-hrEeqRY3026cHCc__dpwXLUAbuSn66dpv_FRsV5KSFmldAL4y4_szb_TbTOZdS00it3qYMqv-p-E7z3OXfzOE6UVI
Sending here - token size: 2
My API key: AIzaSyDveofcE52FAIVg5ZCuC9VpiRwNLNIVAJw
<HTML>
<HEAD>
<TITLE>The request was missing an Authentication Key (FCM Token). Please, refer to section &quot;Authentication&quot; of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>The request was missing an Authentication Key (FCM Token). Please, refer to section &quot;Authentication&quot; of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

Error sending Android message: The request was missing an Authentication Key (FCM Token). Please, refer to section "Authentication" of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.
Attempting to send after error #: 1

Second Attempt of Message 3 - after detecting error in JobDone()


send text: now this
SELECT * FROM tokens WHERE updated
Row item: APA91bGBeQfIgbO_Z-bi0_adZidwMoEfV5JsenLMZnqmLVjT2zfKwGIl7l2zEjhUkFfwGq7UZ7A9XiZoK2P3wsFj1O72TAv7Tq_Xb-IxIxsiaRcYW5f8n_A
Row item: APA91bEfXcO-0X0CwVQxIWr29KZFt-hrEeqRY3026cHCc__dpwXLUAbuSn66dpv_FRsV5KSFmldAL4y4_szb_TbTOZdS00it3qYMqv-p-E7z3OXfzOE6UVI
Sending here - token size: 2
My API key: AIzaSyDveofcE52FAIVg5ZCuC9VpiRwNLNIVAJw
Message was sent successfully


Message 4:
send text: new one to go
Row item: APA91bGBeQfIgbO_Z-bi0_adZidwMoEfV5JsenLMZnqmLVjT2zfKwGIl7l2zEjhUkFfwGq7UZ7A9XiZoK2P3wsFj1O72TAv7Tq_Xb-IxIxsiaRcYW5f8n_A
Row item: APA91bEfXcO-0X0CwVQxIWr29KZFt-hrEeqRY3026cHCc__dpwXLUAbuSn66dpv_FRsV5KSFmldAL4y4_szb_TbTOZdS00it3qYMqv-p-E7z3OXfzOE6UVI
Sending here - token size: 2
My API key: AIzaSyDveofcE52FAIVg5ZCuC9VpiRwNLNIVAJw
<HTML>
<HEAD>
<TITLE>The request was missing an Authentication Key (FCM Token). Please, refer to section &quot;Authentication&quot; of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>The request was missing an Authentication Key (FCM Token). Please, refer to section &quot;Authentication&quot; of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

Error sending Android message: The request was missing an Authentication Key (FCM Token). Please, refer to section "Authentication" of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.
Attempting to send after error #: 1

Second Attempt of Message 4 - after detecting error in JobDone()
send text: new one to go
Row item: APA91bGBeQfIgbO_Z-bi0_adZidwMoEfV5JsenLMZnqmLVjT2zfKwGIl7l2zEjhUkFfwGq7UZ7A9XiZoK2P3wsFj1O72TAv7Tq_Xb-IxIxsiaRcYW5f8n_A
Row item: APA91bEfXcO-0X0CwVQxIWr29KZFt-hrEeqRY3026cHCc__dpwXLUAbuSn66dpv_FRsV5KSFmldAL4y4_szb_TbTOZdS00it3qYMqv-p-E7z3OXfzOE6UVI
token too small - no send here.. 2
Sending here - token size: 2
My API key: AIzaSyDveofcE52FAIVg5ZCuC9VpiRwNLNIVAJw
Message was sent successfully
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Push server worked.

The Firebase solution presents this problem (as I see it - which may be wrong):
I register one project with Firebase.

I have many companies that share the same base android app and B4J server.
Each company has many driver's and vehicles.
If I send a message for one vehicle - All vehicles for every company will receive the message.
Sure, I can check who is logged in / and what vehicle / what company by adding extras to the message, so only the desired recipient sees it - but this seems wasteful.

With B4X, the device sends the token along with the current driver, vehicle it is in, and the company it is registered to.
When these values change, I update the tokens table with new results. Then message sender can pick one (or many) receipts and send only to them.

Thanks
 
Upvote 0
Top