Android Question GCM - getting message without text

Shay

Well-Known Member
Licensed User
Longtime User
Hi

I am using the GCM library here, and it is working fine while using the desktop cmd
once I am trying to implemete php that will send the GCM messgae, (using many site examples)
such as:
http://www.androidhive.info/2012/10...ing-google-cloud-messaging-gcm-php-and-mysql/
or:
https://github.com/mattg888/GCM-PHP-Server-Push-Message/blob/master/GCMPushMessage.php

the message arrive (I am getting the new message arrived + icon) but it is withot any message (text)

B4X:
Sub MessageArrived (Intent As Intent)
    Dim From, CollapseKey, Data, Msg As String 'ignore
    If Intent.HasExtra("from") Then From = Intent.GetExtra("from")
    If Intent.HasExtra("data") Then Data = Intent.GetExtra("data")
    If Intent.HasExtra("msg") Then Msg = Intent.GetExtra("msg")
    If Intent.HasExtra("collapse_key") Then CollapseKey = Intent.GetExtra("collapse_key")

    Dim n As Notification
    n.Initialize
    n.Light = False
    n.Vibrate = False
    n.Icon = "icon"
    n.SetInfo("Enail", Data, "Main") 'Change Main to "" if this code is in the main module.
    n.notify(Num_notify)
    Num_notify=Num_notify+1

    Log ("Data=" & Data)
    Log ("From=" & From)
    Log ("CollapseKey=" & CollapseKey)
    Log ("Msg=" & Msg)
  
    'Here you should handle the new message:
    Log("New message arrived: " & Data)
    ToastMessageShow("New message: " & Data, True)
End Sub

I am only getting the icon, from, and collapseKey, no msg or data

I think the issue is with the library implementation, since it cannot be that all examples are not working
any idea?
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
I´m using this php-code to send and it´s working fine for me here

PHP:
$url = 'https://android.googleapis.com/gcm/send';
  $fields = array(
      'registration_ids'  => $regIDs,
      'data'              => array( "message" => $message, 'tickerText' => $Ticker, 'contentTitle' => "Title", "contentText" => "ContentText" ),
  );

  $headers = array(
      'Authorization: key=' . "your auth key here...",
      '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_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

  $result = curl_exec($ch);
  #  fwrite($file_handle, date("d.m.Y H:i:s", time()).": Curl-Result: ".$result."\r\n");
  if ($result === FALSE) {
        #fwrite($file_handle, date("d.m.Y H:i:s", time()).": Error: ".curl_error($ch)."\r\n");
    //die('Problem occurred: ' . curl_error($ch));
  }
  curl_close($ch);
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
also your code, does not provide the $message
any idea? maybe something on server side?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
also your code, does not provide the $message
any idea? maybe something on server side?

You dont get "data"... Data is the array google is using to put in the intent-extras.
Everything that you put into data on php-side you will get in the intents extras.

PHP:
function PushNotification($message, $title){
    echo "Title: ".$title."\r\n";
    echo "Message: ".$message."\r\n";

    // Replace with real BROWSER API key from Google APIs
    $apiKey = "AIza_apikey";

    // Replace with real client registration IDs
    $registrationIDs = array( "id of destination" );

    // Set POST variables
    $url = 'https://android.googleapis.com/gcm/send';

    $fields = array(
       'registration_ids'  => $registrationIDs,
      'data'              => array( "message" => $message, "title" => $title, "othervalue1" => 100, "othervalue2" => 815 )
  );

    $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_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

    // Execute post
    $result = curl_exec($ch);

    // Close connection
    curl_close($ch);

    return $result;
}

PushNotification("This is the messagetext","this is the title");

B4X:
Sub MessageArrived (Intent As Intent)
    Log("MessageArrived()")
    Dim From, CollapseKey, Data, message, title, othervalue1, othervalue2 As String
    If Intent.HasExtra("message") Then
        message = Intent.GetExtra("message")
        Log("Message: "&message)
    End If
    If Intent.HasExtra("title") Then
        title = Intent.GetExtra("title")
        Log("Title: "&title)
    End If
    If Intent.HasExtra("from") Then
        From = Intent.GetExtra("from")
        Log("From: "&From)
    End If
    If Intent.HasExtra("othervalue1") Then
        othervalue1 = Intent.GetExtra("othervalue1")
        Log("othervalue1: "&othervalue1)
    End If
    If Intent.HasExtra("othervalue2") Then
        othervalue2 = Intent.GetExtra("othervalue2")
        Log("othervalue2: "&othervalue2)
    End If

    If Intent.HasExtra("data") Then
        Data = Intent.GetExtra("data")
        Log("Data: "&Data)
    End If
    If Intent.HasExtra("collapse_key") Then
        CollapseKey = Intent.GetExtra("collapse_key")
        Log("CollapseKey: "&CollapseKey)
    End If
  
    If Data <> "" Then
    End If
End Sub

** Service (pushservice) Create **


** Service (pushservice) Start **


MessageArrived()
Message: This is the messagetext
Title: this is the title
From: 79876679508
othervalue1: 100
othervalue2: 815
CollapseKey: do_not_collapse
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
Thanks ManFred !!!
your full example works, (B4A side fixed it)
I guess the difference is the HasExtra and not GetExtra
 
Upvote 0
Top