iOS Question What is the best way ?

tufanv

Expert
Licensed User
Longtime User
Hello,

I am planning to implement different group for my current app with firebase notifications. What i want to do is , I want to be able to send push messages to members with active subscriptions , and without subscriptions seperately.

Currently we are using :
B4X:
Private Sub fm_FCMConnected
   Log("FCMConnected")
   'here we can subscribe and unsubscribe from topics
   fm.SubscribeToTopic("ios_general") 'add ios_ prefix to all topics
End Sub

but i think this event fires as soon as the applicaiton is active so it is before the check of subscription status. How can i change the method to subscribe to paidmembers topic if the subscription is active , if the subscription ended delete from paidmembers topic and subscribe to freemembers topic.

I cant use fm.subscribetopic or unsucbscribe from outside the fcmconnected event also .

I would be happy if you can give a clue about it .Thanks

edit: Also lets say the users subscription has ended but he did not launch the app on purpose to continue receive push messages like a subscribed member. İf he does not launch the app , i wont be able to check if the users subscription has ended so i wont be able to delete him from the paidmembers topic and he will continue to receive push messages for the paid members ( do not think these messages like advertising these are premium push messages to send news for example )
 
Last edited:

tufanv

Expert
Licensed User
Longtime User
Android notifications are always treated as silent push notifications. It is your B4A code that shows the notifications.
B4X:
Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)
   Log($"Message arrived: ${Message}"$)
'  Msgbox(Message, "Push message!")
Dim connection As HttpJob
                     connection.Initialize("durumcheck",Me)
                     connection.download2("https://www.ctmobilesoft.com/tahminhaneapp/tahminhane.php", Array As String ("action", "8", "girdi1", txtusername.text, "girdi2", txtpassword.text,"girdi3",gecerliid,"girdi4",cihazno))
   CompletionHandler.Complete
End Sub

I have this sub in my main app and i built in release mode. In jobdone i have :

B4X:
If Job.JobName="durumcheck" Then
    If Job.Success = True Then
                    Dim res As String
                    Dim m As Map
                    res = Job.GetString
                    Dim parser As JSONParser
                    parser.Initialize(res)
                    Log("Response from server: " & res)
                    If res.Contains("tahmin") Then
                            Dim ln As Notification
    ln.Initialize(DateTime.Now + 6 * DateTime.TicksPerSecond) '6 seconds from now
    ln.IconBadgeNumber = 1
    ln.AlertBody = "parali uye"
    ln.PlaySound = True
    ln.Register
                    End If
End If
End If

and with the sending tool i have :

B4X:
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 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)
        m.Put("content_available", True)
    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

when i send message i think jobdone is not tiriggered or even it is triggered i dont get the lolcal notification. Where am i wrong on this. ?
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
i think it is because of the line :

connection.download2("https://www.ctmobilesoft.com/tahminhaneapp/tahminhane.php", ArrayAsString ("action", "8", "girdi1", txtusername.text, "girdi2", txtpassword.text,"girdi3",gecerliid,"girdi4",cihazno))

i changed the gecerliid and cihazno to actual numbers like "1" and "1" and it works now ! thanks for your help
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Make sure to test it in release mode and don't explicitly kill the app.
I am testing in release mode but lets say 1 message arrived i close the app with hıome button and the next one i sent is not coming. i wll test further and report back.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
I have made 2 test. In the first one messaage came and local notification popped up. in the second try nothing happened. I did nothing different. I installed the app un oıne time and close with home button only and then sent the message..
Show a local notification to test it.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Show a local notification to test it.
in the last 2 tests it worked again. I have another question
when sending the message we comment out the
B4X:
        Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")
    '    m.Put("notification", iosalert)

so how does the sender send the message body ? We just send content available to system. How can i read the body and show it as local notification ? messages map does not contain a body key when we comment out the above line i think ?
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
You can see the message structure in the logs:
B4X:
Log($"Message arrived: ${Message}"$)

Run it in debug mode if you are unable to get iReleaseLogger to work.

You can also do something like:
B4X:
n.AlertBody = Message 'just to see the structure
ok i got it thanks . What i did is under remotenotifications :

mybody = messages.get("body")
then i showed localnotification with
n.AlertBody = mybody

Thanks Erel !
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
You can see the message structure in the logs:
B4X:
Log($"Message arrived: ${Message}"$)

Run it in debug mode if you are unable to get iReleaseLogger to work.

You can also do something like:
B4X:
n.AlertBody = Message 'just to see the structure

Some messages not arrive and some are arriving. With release logger i saw that , when the message does not come it actualy comes but does not trigger the httpjob and also gives an exception error and app is not configured error as in the image below :

errorlog.png
 
Upvote 0
Top