B4J Question [ABMaterial] Need Simple example for Google Cloud Message

Harris

Expert
Licensed User
I suspect the B4X Push Server will be required... somewhere.

On the Android side, I can stuff new generated messages (outgoing) into a local table and using RDC, send back to server - No GCM required - I suspect.

My Android app currently registers to GCM correctly, and then tries to send using (php - bad):
jb.PostString(thesite&"/recmess.php?ar1="&dt&"&ar2="&nam&"&ar3="&mes&"&ar4="&comp_id&"&ar5="&DefCM.lat1&"&ar6="&DefCM.lon1 ,"")

It should receive messages from GCM without any issues since it just handles the incoming GCM, as it did in the past.


On the ABM/B4J server side, I would like to push these out using GCM.
This is my old code using PHP, which I would like to get rid of...

B4X:
function sendGCM($UserID, $message) {
    $query = "SELECT id From devices Where name=$UserID";
    $row = mysql_fetch_object(mysql_query($query));
   
    // Replace with real BROWSER API key from Google APIs
    $apiKey = "AIzaSyAwsxxxxxxxxxxxxxxO3k"; // Browser Key
   
    // Replace with real client registration IDs
    $registrationIDs = array( $row->id );
       
    // Set POST variables
    $url = 'https://android.googleapis.com/gcm/send';
   
    $fields = array(
                    'registration_ids'  => $registrationIDs,
                    'data'              => array( "data" => $message ),
                    );
   
    $headers = array(
                        'Authorization: key=' . $apiKey,
                        'Content-Type: application/json'
                    );
   
    // Open connection
    $ch = curl_init();
   
    // Set the url, number of POST vars, POST data
    curl_setopt( $ch, CURLOPT_URL, $url );
   
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
   
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
   
    // Execute post
    $result = curl_exec($ch);
   
    // Close connection
    curl_close($ch);
}
Thanks
 

Harris

Expert
Licensed User
Got PushServer working and understood. Had to get new legacy keys from Google by starting a new Firebase project. So be it...
Have one small issue sending messages from B4J (ABM) app. Posted new question about that.
 

Harris

Expert
Licensed User
Sending new message...
Works first and second, then fails to send with result below.

If I wait fot 1 minute, then usually it will send.

Weird?



B4X:
<HTML>

<HEAD>
<TITLE>The request was missing an Authentication Key (FCM Token). Please, refer to section &quot;Authentication&quot; of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>The request was missing an Authentication Key (FCM Token). Please, refer to section &quot;Authentication&quot; of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

Error sending Android message: The request was missing an Authentication Key (FCM Token). Please, refer to section "Authentication" of the FCM documentation, at https://firebase.google.com/docs/cloud-messaging/server.
 
Top