Android Question B4A Firebase Push Notifications 2023+ Sending

asales

Expert
Licensed User
Longtime User
You can't.
Check this thread:
 
Upvote 0

javiito

Member
Licensed User
Longtime User
Hi,
So, there is no way to send push messages from android application like before? I am confused because in the ititial forum ([B4X] Firebase Push Notifications 2023+) someone says that it works for him in an b4a application.
Thanks!
 
Upvote 0

asales

Expert
Licensed User
Longtime User
Hi,
So, there is no way to send push messages from android application like before?
No.
I am confused because in the ititial forum ([B4X] Firebase Push Notifications 2023+) someone says that it works for him in an b4a application.
Thanks!
For now, I think the final answer is this from Erel:
"The underlying libraries are not compatible with Android."
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Firebase General Send Functions in Using APIs in PHP
ref.
PHP:
<?php
function signUp($email, $password) {
    $url = 'https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=[API_KEY]';
    $data = json_encode([
        'email' => $email,
        'password' => $password,
        'returnSecureToken' => true
    ]);

    $options = [
        'http' => [
            'header'  => "Content-type: application/json\r\n",
            'method'  => 'POST',
            'content' => $data,
        ],
    ];

    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return json_decode($result);
}

$response = signUp('[email protected]', 'user_password');
print_r($response);
?>

<?php
function signIn($email, $password) {
    $url = 'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=[API_KEY]';
    $data = json_encode([
        'email' => $email,
        'password' => $password,
        'returnSecureToken' => true
    ]);

    $options = [
        'http' => [
            'header'  => "Content-type: application/json\r\n",
            'method'  => 'POST',
            'content' => $data,
        ],
    ];

    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return json_decode($result);
}

$response = signIn('[email protected]', 'user_password');
print_r($response);
?>

<?php
function verifyIdToken($idToken) {
    $url = 'https://identitytoolkit.googleapis.com/v1/accounts:lookup?key=[API_KEY]';
    $data = json_encode([
        'idToken' => $idToken
    ]);

    $options = [
        'http' => [
            'header'  => "Content-type: application/json\r\n",
            'method'  => 'POST',
            'content' => $data,
        ],
    ];

    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return json_decode($result);
}

$response = verifyIdToken('ID_TOKEN_FROM_CLIENT');
print_r($response);
?>

<?php
function sendNotification($to, $title, $body) {
    $url = 'https://fcm.googleapis.com/fcm/send';
    $apiKey = 'YOUR_SERVER_KEY';

    $fields = json_encode([
        'to' => $to,
        'notification' => [
            'title' => $title,
            'body' => $body
        ]
    ]);

    $headers = [
        'Authorization: key=' . $apiKey,
        'Content-Type: application/json'
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    curl_close($ch);
    return $result;
}

$response = sendNotification('DEVICE_TOKEN', 'Test Title', 'Test Body');
print_r($response);
?>
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
I think it is not a good idea to use Android or B4A app as a sending tool because it expose to a bad actor can extract the json file and create another app to send notification or message to the users of the original app.

It is more secure to let another server do the sending job.

B4A just need to call this server to make the API call to Firebase and send the message.
 
Upvote 0

javiito

Member
Licensed User
Longtime User
Hi again. With the new information from Erel, I still ges an error when trying to get the token. The error arises when this line is executed:

GoogleCredentials.InitializeStatic("com.google.auth.oauth2.GoogleCredentials"):

java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/Duration;
at com.google.auth.oauth2.OAuth2Credentials.<clinit>(OAuth2Credentials.java:70)
at java.lang.Class.classForName(Native Method)
...

Any help?
 
Upvote 0
Top