iOS Question How manage toen for push notification

Marco Maria Vilucchi

Active Member
Licensed User
Longtime User
Hi
I'm confused.
In this trade:
https://www.b4x.com/android/forum/t...ions-push-messages-server-not-required.68645/
Erel says: You can handle the Application_PushToken event if you want to access the token
In the same trade says:
upload_2017-8-3_16-49-27.png


I'm using this:
B4X:
Private Sub Application_PushToken (Success As Boolean, Token() As Byte)
    If Success Then
        Dim bc As ByteConverter
        Dim j As HttpJob
        Tokenid=bc.HexFromBytes(Token)
        Log("Tokenid: " & Tokenid)
    Else
        Log("Error getting token: " & LastException)
    End If
End Sub

Then I save my token on mysql.

I used SendingTool to test notification and it works with "ios_general"
How can I use token testing with SendingTool?

Is correct to save on mysql and then use hte tokenid formatted like it?
Tokenid=bc.HexFromBytes(Token)

After this test i need to send push notification from a php script.
Tks
Marco
 

Marco Maria Vilucchi

Active Member
Licensed User
Longtime User
I tried but I have this:
{"multicast_id":4973443348501040409,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

My code is:

B4X:
'Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private const API_KEY As String = "AAAA9awD0BQ:APA91bGDh0YT4p9AVQi18x0pufJ63gUedQtzq7nWtqBE9H1VlJHTRWqpmjeWNIeIs_dk0Rcw6LohJIGjdCoE1TPspSGZsLo9kq0qzDKBNheL9uRlQdJozKSRX-DBcQPvuamsAqT_TWBX"
    Private const tokenid As String = "1A796E86FD8BD0DEFA94884C6F96036FD6C65E21B6DA4699DF675C1BB6AEDA00"
End Sub

Sub AppStart (Args() As String)
'    SendMessage("ios_general", "Titolo del Messaggio", "Corpo del Messaggio")
    SendMessage(tokenid, "title", "body")
    StartMessageLoop
End Sub

Private Sub SendMessage(Topic As String, Title As String, Body As String)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
'    Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
    Dim m As Map = CreateMap("to": Topic)
    Dim data As Map = CreateMap("title": Title, "body": Body)
'    If Topic.StartsWith("ios_") Then
        Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")
        m.Put("notification", iosalert)
        m.Put("priority", 10)
'    End If
    m.Put("data", data)
    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=" & API_KEY)
End Sub


Sub JobDone(job As HttpJob)
   Log(job)
   If job.Success Then
     Log(job.GetString)
   End If
   job.Release
   ExitApplication '!
End Sub

My token seems correct....
 
Upvote 0

Marco Maria Vilucchi

Active Member
Licensed User
Longtime User
Sorry,
but as I told in this tread I'm using this code:
B4X:
Private Sub Application_PushToken (Success As Boolean, Token() As Byte)
    If Success Then
        Dim bc As ByteConverter
        Dim j As HttpJob
        Tokenid=bc.HexFromBytes(Token)
        Log("Tokenid: " & Tokenid)
    Else
        Log("Error getting token: " & LastException)
    End If
End Sub
Isn't it correct?

and if I use
B4X:
Private Sub GetToken As String
   Dim no As NativeObject
   Dim token As NativeObject = no.Initialize("FIRInstanceID").RunMethod("instanceID", Null).RunMethod("token", Null)
   If token.IsInitialized Then Return token.AsString Else Return ""
End Sub
I need to cancel Application_PushToken software?
 
Upvote 0

Marco Maria Vilucchi

Active Member
Licensed User
Longtime User
I'm trying to use sub GetToken (Ideleted Sub Application_PushToken).
Using GetToken once I have a string with 152 chars and the next time an empty string.
Then another 152 chars string.... and so on
Whitch is the right method to have Token? (I want register it on a DB)
This is token i have:
fnM-OfWkJCs:APA91bGmH5nynHJtQF43WzzDCVD6aok2k5c-NuUc-5zxoybns-NSxBZIINzq_1Yi4kO5h7dGXjeuKGm1Lrywqc1bnZzh1HXA4d0Bt914MTNbxnn9m768Rt406gaQ9aXSHRKUYWZpbMqf
 
Last edited:
Upvote 0
Top