Android Question Firebase Messaging changes notification when the app closed

amer bashar

Member
Licensed User
Hi all,
It`s a very strange problem and I don't know if I can describe it well,
I am using firebase messaging according to this instructions and it is not useful to upload my code because it`s just the same,
The problem is that I want to custom the notification (for example if a body = "new_call" notify "test")
B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim n2 As Notification
    n2.Initialize2(n2.IMPORTANCE_HIGH)
    n2.Icon = "icon"
    if Message.GetData.Get("body") = "new_call" then
        n2.SetInfo(Message.GetData.Get("title"), "test",  Main)
    Else
        n2.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"),  Main)
    End If
    n2.Notify(1)
End Sub
So there are two situations:
1. Release mode, the app in foreground => the code works fine
2. Release mode, the app in background => the notification shows the message body received even it was "new_call" !
I'm on my way to having a heart attack

Update: I cancelled the if condition like this :
B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim n2 As Notification
    n2.Initialize2(n2.IMPORTANCE_HIGH)
    n2.Icon = "icon"
    n2.SetInfo(Message.GetData.Get("title"), "test",  Main)
    n2.Notify(1)
End Sub
so the result is more confusing now :
1. Release mode, the app in foreground => the notification always "test"
2. Release mode, the app in background => the notification shows the message body received !!!!!!
 
Last edited:

amer kasem

Member
Licensed User
I guess that you aren't sending the messages correctly. Use the B4J code.
Thank you boss, oh I have to use php and it usually works fine
PHP:
function send_notification($title , $msg , $token){
        $body = $msg;
        $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
        $data = [
            'title' => $title,
            'body' => $body,
        ];
        $iosalert = [
            'title' => $title,
            'body' => $body,
            'sound' => "default"
        ];
        $m = [
        'to' => $token,
        'notification' => $iosalert,
        'priority' => 10,
        'data' => $data
        ];
        $headers = [
            'Authorization: key=' . "HERE_MY _SERVER_KEY",
            'Content-Type: application/json;charset=UTF-8'
        ];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $fcmUrl);
        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($m));
        $result = curl_exec($ch);
        curl_close($ch);
}
 
Upvote 1

amer bashar

Member
Licensed User
I tried to use the B4J code and the problem solved, another problem raised that I need to send to a specific user so I put the FCM_Token of the user instead of topic :
B4X:
Send("fHt-0W4FSUu0ggRCfZac3A:APA91bFkFiLWho .. to the end of token", "title", "body")
but it returned with this error :
B4X:
ResponseError. Reason: , Response: {
  "error": {
      "code": 400,
          "message": "Request contains an invalid argument.",
              "status": "INVALID_ARGUMENT",
                  "details": [
                        {
                            "@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
                            "errorCode": "INVALID_ARGUMENT"
                        },
                        {
                        "@type": "type.googleapis.com/google.rpc.BadRequest",
                        "fieldViolations": [
                        {
                            "field": "message.token",
                            "description": "Invalid registration token"
Please can you help me either sending through b4j to FCM_Token or using the php right way?

Edit: I will start a new thread for this
 
Last edited:
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Here is the code from my php Push notification sender. it works fine.

It is uses the Laravel framework, but I think you can just include the classes instead.

The key difference I can see is thet you have not include "/topic/" at the start of the topic, but this may already be included in the string.
What error do you get back from Firebase?

You also need to take a look at this.


B4X:
<?php

namespace App\Http\Controllers;

use App\Models\Festival;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Illuminate\Support\Facades\Log;

class firebasePushController extends Controller
{
    const FirebasePushURL = "https://fcm.googleapis.com";
    const FirebaseSend    = 'fcm/send';


    public static function SendPushNotification($topic, $title, $body, $key)
    {
        Log::debug("Sending Push notification to $topic. Title is $title");
        $client = new Client(['base_uri' => self::FirebasePushURL]);

        $headers = [
            'Content-Type' => 'application/json;charset=UTF-8',
            'Authorization' => 'key=' . $key
        ];

        $sending = [
            'title' => $title,
            'body' => $body,
            'sound' => "default",
            'topic' => $topic
        ];

        $data = [
            'to' => "/topics/$topic",
            'priority' => 10,
            'data' => $sending,  // Android version
            'notification' => $sending, //iOS version
            //                                 'direct_boot_ok' => true
        ];

        log::debug("Sending Push->".json_encode($data));
        $request = new Request("POST", SELF::FirebaseSend, $headers, json_encode($data));

        $response = $client->send($request, ['timeout' => 10]);

        $data = json_decode($response->getBody()->getContents());

        log::debug("Push notification response body ".print_r($data,true));
        return $data;
    }
}
 
Upvote 0

amer bashar

Member
Licensed User
Thank you, I will try it ..
There is no error from Firebase, the notification sent but with the problem I described
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…