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.
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 "Authentication" 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 "Authentication" 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 "Authentication" 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 "Authentication" 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 "Authentication" 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 "Authentication" 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 "Authentication" 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 "Authentication" 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