Android Question Send notification from IOS

stevenindon

Active Member
Licensed User
Hello all, I use below code to send notification (Both IOS and Android). When i send from android to IOS, IOS is working fine. But when i use below code to send from
IOS to Android, The message arrive as notification. But when i click on the (Top drag down) notification, The android application wont launch..

Send Notification Code :

B4X:
'******************************************************
'SEND TO NOTIFICATION GROUP
'******************************************************
Public Sub TW_Notifi_SendGroup(strTopic As String, Title As String, Body As String)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("to": $"/topics/${strTopic}"$)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")
    m.Put("notification", iosalert)
    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=" & TW_Notifikey)
End Sub

Android will only launch correctly (*By clicking on top dropdown notification) ONLY if i take out these 2 lines :

B4X:
'******************************************************
'SEND TO NOTIFICATION GROUP
'******************************************************
Public Sub TW_Notifi_SendGroup(strTopic As String, Title As String, Body As String)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("to": $"/topics/${strTopic}"$)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default") <<<----- TAKE OUT THIS
    m.Put("notification", iosalert) <<<--------- TAKE OUT THIS
    m.Put("priority", 10)
    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=" & TW_Notifikey)
End Sub

Taking out both line above will eventually cause IOS not receive the message... ?
 

stevenindon

Active Member
Licensed User
Hello all,

I have done another test ... this round by using firebase console (Cloud Messaging) to send a test message to both my IOS and Android application.
When i click on top notification, IOS executes (*Starts) the application perfectly... but not on Android. (*Android - after click top drop down notification, application did not start).

Anyone has encounter such problem before?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
There are 2 types for iOS. One is normal notification, another is silent notification with data payload.
 
Upvote 0

stevenindon

Active Member
Licensed User
Aeric.. thanks for the suggestion. My problem now is on Android with firebase notification (From firebase console). When i send a message from firebase console,
I am able to receive the notification... but it wont execute the application by clicking on the arrived notification (*From top notification drop down).

Thought firebase console notification format is pretty standard thing... Same problem when i test sending the notification using erel's B4J example.

Thanks.
 
Last edited:
Upvote 0

stevenindon

Active Member
Licensed User
Erel, Thank you to have pointed out this link. I followed everything and i am able to receive notification message. But the only problem is when i click on the top dropdown notification on my ANDROID, My ANDROID application wont launch (IOS ok). These are my findings.... The launching of ANDROID can only happen if i send using code like this :

B4X:
'******************************************************
'SEND TO NOTIFICATION GROUP
'******************************************************
Public Sub TW_Notifi_SendGroup(strTopic As String, Title As String, Body As String)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("to": $"/topics/${strTopic}"$)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    m.Put("priority", 10)
    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=" & TW_Notifikey)
End Sub

*Note ONLY data in notification (No notification : )

But i need to send using below code (*With notification : ) *For IOS to work
B4X:
'******************************************************
'SEND TO NOTIFICATION GROUP
'******************************************************
Public Sub TW_Notifi_SendGroup(strTopic As String, Title As String, Body As String)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("to": $"/topics/${strTopic}"$)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default") <<<----- WITH THIS FOR IOS TO WORK
    m.Put("notification", iosalert) <<<--------- WITH THIS FOR IOS TO WORK
    m.Put("priority", 10)
    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=" & TW_Notifikey)
End Sub

Once i include the notification : , I will recieve the notification for both ANDROID AND IOS... but ANDROID wont launch when i click on the top drop down notification.

Android will only run if notification contains data : ONLY.... but if there is notification : or both, it doesn't seems to execute when on click top dropdown notification.
 
Last edited:
Upvote 0

stevenindon

Active Member
Licensed User
Dear all, According to Erel's example of sending notification (B4J) as below:

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 <<------------------------------ **NOTICE HERE**
        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

Just to confirm... In erel's example of sending notification, I notice that sending to ANDROID and IOS are set to different Topic. If topic contains "ios_" only send with
m.Put("data", data) AND m.Put("notification", iosalert) to IOS..... else send notification with m.Put("data", data) ONLY to ANDROID.

So that they will work correctly for both IOS and ANDROID... *Correct me if i am wrong. Thanks

ANDROID - Accepts notifications with m.Put("data", data) ONLY
IOS - Accepts notifications with m.Put("data", data) AND m.Put("notification", iosalert)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Dear all, According to Erel's example of sending notification (B4J) as below:

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 <<------------------------------ **NOTICE HERE**
        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

Just to confirm... In erel's example of sending notification, I notice that sending to ANDROID and IOS are set to different Topic. If topic contains "ios_" only send with
m.Put("data", data) AND m.Put("notification", iosalert) to IOS..... else send notification with m.Put("data", data) ONLY to ANDROID.

So that they will work correctly for both IOS and ANDROID... *Correct me if i am wrong. Thanks

ANDROID - Accepts notifications with m.Put("data", data) ONLY
IOS - Accepts notifications with m.Put("data", data) AND m.Put("notification", iosalert)
I don’t think the topic name has any issue. For me, using different topic is for differentiate when at one time I only want to send to a specific platform and not both.

As I mentioned above, you need to understand there are 2 types of push notifications. In Android, there is no big issue to add the map of information or parameters with data but things are a bit different in iOS. If you are having the app open in foreground, there is no notification pop up if you send normal push notification. It also doesn’t carry any data or payload. So in some cases, you should use silent push notification.
The best idea I think work for me is by sending both “normal” notification and “silent” notification so it will work in any situation BUT it will consume 2 push notification “credit”. Google provide you certain number of push notification in 1 year free trial period and will charge a “Pay as you use” if you exceed certain number. I am not sure things have any change recently. That is my experience when implementing Firebase Push Notification a few years ago.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
For Android not work to open the app when tap on the notification, it seems strange to me. My guess is it may depend on phone settings or different manufacturers or the custom Android itself. For example my Xiaomi has different settings for notification to turn on/off sound and vibrate. It may also different in different or newer version of Android.
 
Upvote 0

stevenindon

Active Member
Licensed User
aeric, Thank you for sharing your experience. I have already split topics between Android and IOS to make things work correctly.

ANDROID - "AND_TWGroup" (Sending to this group with data: Only)
IOS - "IOS_TWGroup" (Sending to this group with notification: and datat:)

"For Android not work to open the app when tap on the notification, it seems strange to me. My guess is it may depend on phone settings"
ANSWER :
- aeric, when i send the notification with notification: AND data: , Android just don't execute the app when i tap the top dropdown notification.
- when i send the notification with data: only, Android executes the app perfectly when i tap the top dropdown notification.
* I guess it has nothing to do with my android setting.

All is working as expected for the time being... Just wanted to get a better option out there.

Thanks aeric
 
Upvote 0
Top