Android Question Firebase questions

androb

Member
Licensed User
Longtime User
I have setup a service based on the firebase tutorial. I can send messages from my PHP page successfully. However, when I try and add functionality to the service I have hit a problem.

Its to do with the function Sub fm_MessageArrived (Message As RemoteMessage)

After the notification has been received, none of the code seems to be run.
Here is my code

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

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   Private fm As FirebaseMessaging
 
End Sub

Sub Service_Create
fm.Initialize("fm")
UpdateFCMToken 'directly after fm.initialize
Log (fm.Token)
End Sub


Public Sub UpdateFCMToken
    fm.SubscribeToTopic("general-esp-staff") '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)

    Dim Vibrate As PhoneVibrate ' For phone vibration
    Vibrate.Vibrate (2000) 
    ToastMessageShow ("Notification Received", True)    

End Sub

Sub Service_Destroy

End Sub

I am confused because the device doesn't vibrate or show the toastmessage. Although the notification is in the notification bar and is displayed properly. I can send repeated notifications through GCM and they show on the device without issue, but the toastmessage NEVER shows.

I want to be able to start another app after the notification has been displayed but I am at a loss of how to do this, when none of the code seems to be being run in the function after the n.Notify(1) line.

Is this expected behaviour?
Is it because this code is being run from a service?

Is it because the activity is hidden (using manifest editor)
B4X:
AddReplacement(<action android:name="android.intent.action.MAIN" />, )
AddReplacement(<category android:name="android.intent.category.LAUNCHER" />, )

My goal eventually is to have it display a webview once the notification is clicked on but so far this is really confusing me.

Any help is appreciated.
 

gregorio_adrian_gimenez

Active Member
Licensed User
Longtime User
Hello, I probed my code and it works fine.
Sends the toastmessage and vibrates the device.
I think you could check by placing Dim id As Int = Rnd (1, 0x7FFFFFFF) inside the sub fm_MessageArrived (Message As RemoteMessage)
And put n.notify (id)


Is what I see different from your code, but I'm not sure it's a solution
 
Upvote 0

androb

Member
Licensed User
Longtime User
Thank you for the responses.

If the notify app is in the background, the device does not vibrate, although the notification arrives. If the activity is showing, then device does vibrate when the notification is received, however the notification has "null" listed as the notification title and body. I would like to fix this if possible, but have no idea how. In the debug log, it shows the notification as empty. This is for a message alerting app, so having a message alert only when the app is running in the foreground is useless to me.

I ended up adding another service using the NotificationListener library.

Now it does the following:

If the notify app activity is in the foreground, then the first service handles it, device vibrates, but notification body is null
If the notify activity is not showing, then NotificationListener takes care of it and vibrates/sounds when the notification is received and runs the notify app.

The notification is sent from my PHP web app using CURL, I also store this message in a database. so having it changed to "null" is not that important, as the client app retrieves and displays it from the database, the notification just acts as a notice to say a message is waiting. Although it would be good to know why it has both "null" for title and body for the notification.

For Now, I have a solution. Although it is far from ideal to have to install two services.
 
Upvote 0

androb

Member
Licensed User
Longtime User
This is my PHP code for sending a message

B4X:
function GCM_curl_request($info)
{
    $topic   = $info['topic'];
    $message = $info['message'];
   

$msg = array(
    'to'          => '/topics/' . $topic,
    'notification' => array(
                            'body'      => $message,
                            'title'     => 'ESPNotify',
                            'icon'      => "ic_launcher"
    )
);

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

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://gcm-http.googleapis.com/gcm/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( $msg) );
$result = curl_exec($ch );
curl_close( $ch );
return $result;

}
 
Upvote 0
Top