Android Question send fcm message with php problem

tufanv

Expert
Licensed User
Longtime User
Hello,

I Have followed the topic https://www.b4x.com/android/forum/threads/b4x-send-firebase-message-via-php.69066/#post-600747

there is no problem at any stage except the message body comes null. There is no problem with the title. The code i used in the php is the code in the post above.

My messagearrived sub is the same as Erel's tutorial.

Notification arrives and fires , title is ok but body is null. php code is:

B4X:
<?php

$action=$_GET["Action"];


switch ($action) {
    Case "M":
         $r=$_GET["r"];
        $t=$_GET["t"];
        $m=$_GET["m"];

        $j=json_decode(notify($r, $t, $m));

        $succ=0;
        $fail=0;

        $succ=$j->{'success'};
        $fail=$j->{'failure'};

        print "Success: " . $succ . "<br>";
        print "Fail   : " . $fail . "<br>";

        break;


default:
        print json_encode ("Error: Function not defined ->" . $action);
}

function notify ($r, $t, $m)
    {
    // API access key from Google API's Console
        if (!defined('API_ACCESS_KEY')) define( 'API_ACCESS_KEY', 'my access key here');
        $tokenarray = array($r);
        // prep the bundle
        $msg = array
        (
            'title'     => $t,
            'message'     => $m,
            'MyKey1'       => 'MyData1',
            'MyKey2'       => 'MyData2',

        );
        $fields = array
        (
            'registration_ids'     => $tokenarray,
            'data'            => $msg
        );

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

        $ch = curl_init();
        curl_setopt( $ch,CURLOPT_URL, '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 );
        return $result;
    }


?>
 

tufanv

Expert
Licensed User
Longtime User
First step is to send with the B4J code. It will work properly. The next step is to compare the json string created by the B4X code to your PHP code.
Thanks. The problem was php code was using message instead of body field.
 
Upvote 0
Top