iOS Question Firebase-push doesn't work for more than three tokens

schimanski

Well-Known Member
Licensed User
Longtime User
I use a server to send push-messages to different devices. For iOS-devices, I use the firebase-push-service. Every IOS-device will use the push-token to register to fcm with:

B4X:
fm.SubscribeToTopic(File.ReadString(File.DirLibrary, "token.dat"))

In the following subs, I collect the ios-tokens to send the message to this. Now, after many units changed from android to ios, we mentioned, that it is not possible with this code to send a push to more than 3 devices. Up to 3, the messages are send without problems. More than 3, I get an 'Bad Request' in the Job_done-event. Is it not possible to send a push to multiple devices? For my way, it is better to collect different device-tokens instead of using main-topics...

B4X:
   Case ID.StartsWith("ios:")
   ID=ID.Replace("ios:", "")
   iosTokens.Add(ID)
   If iosTokens.Size > 900 Then CallSubDelayed3(iOSPushFirebase, "SendMessageTo", iosTokens, msg)

B4X:
Public Sub SendMessageTo(Devices As List, msg As Map)
   Try
     'Sammeln der tokens, an die die Nachrichte gesendet werden soll:
     Dim sb As StringBuilder
     sb.Initialize
     For Each t As String In Devices
      sb.Append($"'${t}' in topics ||"$)
     Next
     sb.Remove(sb.Length-3, sb.Length) 'will fail if topics is empty
     Dim m As Map = CreateMap("condition": sb.ToString)
     
     Dim Job As HttpJob
     Job.Initialize("fcm", Me)
     
     m.Put("data", msg)
     
     If msg.get("steuerbefehl")<>"" Then
       m.Put("priority", 10)
       m.Put("content_available", False)
     Else
       
       Dim notifytitle As String
       notifytitle=msg.Get("absender")
       
       'Für Nichtsteuerbefehle und Schlagzeilen wird eine RemoteNotication vom Server ohne Ton gesendet.
       Dim iosalert As Map =  CreateMap("title": notifytitle, "body": "neue Informationen")
       m.Put("notification", iosalert)
       m.Put("priority", 10)
       m.Put("content_available", True)
      
     End If
     
     Dim jg As JSONGenerator
     jg.Initialize(m)
     Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
     Job.GetRequest.SetContentType("application/json;charset=UTF-8")
     Job.GetRequest.SetHeader("Authorization", "key=" & Main.FirebasePushApiKey)
   Catch
     Main.Logger.logsFATAL(LastException.Message)
   End Try
End Sub

Sub JobDone(job As HttpJob)
   If job.Success=False Then
     Main.Logger.logsInfo("iOS-Geraet(e) konnten nicht gepusht werden! " & job.ErrorMessage)   
     End If
     job.Release
End Sub
 

schimanski

Well-Known Member
Licensed User
Longtime User
Yes, the token is unique. It is the token, which the device gets in the Application_PushToken after registration and I use this token as topic. I think, that's not a good idea:confused:
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
I have read the documentation, but for me, it is not clear, if the device-registration-token is the same as the registration_id.
I tried

B4X:
Dim t as string = ios_token
Dim m As Map = CreateMap("registration_ids": t)

I don't understand, how to implement it...
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
I have tried it over your SendingTool with the token which I get from the the

B4X:
Private Sub Application_PushToken(Success as Boolean, Token() as Byte)
    If Success then
        Dim bc as ByteConverter
        dim ID as string = bc.HexFromBytes(Token)       '=48CE7F765C608CA8C391E1275756C7DC2C6784D65240B1D220XXXXXXXX"
        ...

and then

B4X:
  Dim m As Map = CreateMap("registration_ids": Array("48CE7F765C608CA8C391E1275756C7DC2C6784D65240B1D220XXXXXXXX"))
  ...
instead of the topic-code.

I get this failure:

B4X:
{"multicast_id":5488704055604325806,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

I tried it with different devices and I registered them all new in fcm..
In the client-code, I deleted the line

B4X:
fm.SubscribeToTopc(...
 
Upvote 0
Top