Android Question Android push notification (GCM) - -Information

Humberto

Active Member
Licensed User
Longtime User
I´m trying to implement the GCM message and everthing was OK but I could not receive the message.

The message was always blank

Then I change the line in "Sub MessageArrived (Intent As Intent)" and the message start coming in a different field

B4X:
change
    If Intent.HasExtra("data") Then Data = Intent.GetExtra("data")
to
    If Intent.HasExtra("message") Then xMens = Intent.GetExtra("message")

I´m testing with Samsub Galaxy III
 

Humberto

Active Member
Licensed User
Longtime User
B4X:
<?php

$xip = $_SERVER["REMOTE_ADDR"];
$xnome = $_SERVER["HTTP_USER_AGENT"];
$xhora  = date('d/m/Y h:i:s');

$xmessage = "$xhora \n Server: $xnome \n IP: $xip  ";
// Replace with real GCM browser / server API key from Google APIs
$apiKey = "xxxxxx";
// Replace with real client registration IDs, most likely stored in your database
// Payload data to be sent
$id_cliente = "APA91bEWmyg9WMLQHllaiTGaEIFmpgH606c7G3tPWzqinmKzJDupaPCFxkulkc9xzLpxlRJT39SFTUgM3IE8qdJ3UE0ZnV-LwVBLgPHlpu_F7KmWyPEKLSI";

$xdata = array( 'message' => $xmessage );
// Set request URL to GCM endpoint

$url = 'https://gcm-http.googleapis.com/gcm/send';
// Set POST variables (device IDs and payload)
$fields = array(
                'to'  => $id_cliente,
                'data'  => $xdata,
                );

// Set request headers (authentication and payload type)
$headers = array(
                    'Authorization:key=' . $apiKey,
                    'Content-Type: application/json'
                );

// Open connection
$ch = curl_init();  // Set the url
curl_setopt( $ch, CURLOPT_URL, $url );    // Set request method to POST
curl_setopt( $ch, CURLOPT_POST, true );    // Set custom headers
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);  // Get response back as string
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );  // Set post data
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );    // Send the request
$result = curl_exec($ch);
// Close connection
curl_close($ch);
// Debug GCM response
echo $result;

?>

Sample in PHP.
I´m receiving the text in "message" instead "data".
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
xdata = array( 'message' => $xmessage );
// Set request URL to GCM endpoint

$url =
'https://gcm-http.googleapis.com/gcm/send';
// Set POST variables (device IDs and payload)
$fields =
array(
'to' => $id_cliente,
'data' => $xdata,
);


B4X:
#$xdata = array( 'message' => $xmessage ); // No need to add a new array!!!
// Set request URL to GCM endpoint

$url = 'https://gcm-http.googleapis.com/gcm/send';
// Set POST variables (device IDs and payload)
$fields = array(
                'to'  => $id_cliente,
                'data'  => $xmessage ,
                );
 
Upvote 0

Humberto

Active Member
Licensed User
Longtime User
DonManfred Sending as you said I got this error message

"Field "data" must be a JSON array: 19/04/2016 08:39:25 Server: IP: 127.0.0.1"
 
Upvote 0

Humberto

Active Member
Licensed User
Longtime User
I tested with Moto X android 6 and Samsung S3 androi 4.3 and works but receiving information in a "message" field

I just open this thread if someone find problem receiving information from GCM.
 
Upvote 0
Top