Android Question send push notification Firebase

ilan

Expert
Licensed User
Longtime User
hi

how can i send from my B4A app a push notification?

i try this:

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)
   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

but Job.GetRequest doesnot have a "SetContentType" or "SetHeader" option
only in b4j it has

(ps: the lib i am using is okHttpUtiils)

thanx ilan
 

DonManfred

Expert
Licensed User
Longtime User
but Job.GetRequest doesnot have a "SetContentType" or "SetHeader" option
only in b4j it has
Really?

Are you sure you are using the most uptodate libs?
I get no error using this code...
httpjob0115.png
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
but the error was because i only checked OkHttpUtils

That happened even to me from time to time. Example: I wonder why I can't set the request timeout :)

Hint: If possible send it from php or encrypt it because the Server-Key should be kept secret (inside an app it can easily be extracted)

PHP:

B4X:
function notify ($devt, $myid, $mess)
    {
    print 'In';
    // API access key from Google API's Console
        if (!defined('API_ACCESS_KEY')) define( 'API_ACCESS_KEY', 'AAAAMRuxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
        $tokenarray = array($devt);
        $data = array
        (
            'fromid' => $myid,
            'mess' => $mess
        );
        // prep the bundle
        $msg = array
        (
            'message'     => $mess   
        );
        $fields = array
        (
            'to'     => $devt,
            'data'    => $data
        );
        
        $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;
    }

call it with

B4X:
print notify($targettoken, $myid, $mess);
 
Upvote 0
Top