Android Question Firebase Messaging Bug?

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hi,

I implemented FCM in a app with success but when any activity is open the notification fields are null. If the app is in background, the notification shows correctly the title and message.

Any suggestion about what could be happening? The FirebaseMessaging (exactly this service name as indicated in tutorial ) service code is:

B4X:
#Region  Service Attributes
    #StartAtBoot: True
    
#End Region

Sub Process_Globals
    Public fm As FirebaseMessaging
End Sub

Sub Service_Create
    
    fm.Initialize("fm")
    fm.SubscribeToTopic("general")
    
End Sub

Public Sub SubscribeToTopics
    
    fm.SubscribeToTopic("general") 'you can subscribe to more topics
    
End Sub

Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized And fm.HandleIntent(StartingIntent) Then Return
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
    n.Notify(1)
    
End Sub

Sub Service_Destroy

End Sub
 

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hi @Erel ,

thanks for answering! No, I'm using a PHP standard code (follows). The point is: the message always arrives - when the activity is open (doesn't matter what activity, the problem occurs simply when the app is running) the title and header are null (I can see in log and notification). When is closed or background, title and body are correct. That's it! Only for test purposes, I registered the app in general group and I'm sending messages to general group also.

PHP:
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'XXXXXXXXXXXXXXXXXX' );
$registrationIds = array( TOKENS );
// prep the bundle
$msg = array
(
    'body'  => 'mensagem via api',
    'title'     => 'Hello from Api'
    //'vibrate'   => 1,
    //'sound'     => 1,
);

$fields = array
(
    'to'  => '/topics/general',
    'notification'          => $msg
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json;charset=UTF-8'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello @DonManfred . Thanks, I'll check (besides I only use firebase messaging to trig the service that requests from my server the messages and so the event, even with null fields, is enough).
But its very strange and not logical: if the fields are populated when the activities are closed or paused, why doesn't that happens when they are open? For me looks that some process is "consuming" the data before the firebase messaging startservice acquires and shows. Maybe the own service is being started two times, and the resulted showed by app is the second... what do you think? Is there any way to check in B4A if is that what's happening?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This issue was raised many many times. The B4A code expects a message with a specific structure. If you use the B4J code it will work (and if not then we can continue to help you as we know that the problem is not in the sending code). Once you see that it works you can go over the B4J code and compare it to your code.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hi @Erel . You're right, the problem is the message packet format generated by php and the way that the B4A library parses it. I tested (not with Java but using another android device and using the send code in B4A) and it works (even with activity open in target). Now, I'll verify the java code that u use to send in B4J and modify the message packet format in php.

Thanks!
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hi @Erel . Follows the right code. The problem was one of message keys used. The B4A library searches for "data" and the php code has "notification" key. This is the code to help the community :
B4X:
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'TYPE_HERE_YOUR_FCM_APIKEY' );
$registrationIds = array( TOKENS );
// prep the bundle
$msg = array
(
    'body'  => utf8_encode('mensagem via api'),
    'title'     => utf8_encode('Hello from Api')
    //'vibrate'   => 1,
    //'sound'     => 1,
);

$fields = array
(
    'data'          => $msg,
    'to'  => '/topics/general'
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json;charset=UTF-8'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>
 
Upvote 0
Top