• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Question about Google Cloud Messaging

I have made a droid app that sends messages to an asp.net page on my web server (or maybe it does). When receiving the message, the web app takes the payload that is supposed to be in it, and echos it back to the phone along with it's server time added The droid app doesn't bomb or fail and neither does the web app, but I don't know if the message actually reaches the web server or if it does, then my web app is looking for the wrong post variable. I also think my droid app is looking for a post variable that is never there. So i think that my problem is that I do not understand the format of the messages that are to be expected on either the web app or the droid app. Sending seems to be fine both ways, but receiving correctly does not work in both cases or I just don't understand how to extract the correct thing.

Here's where the droid app sends to gcm

public Void sendmsg(final String latlng){
new AsyncTask<String, String, String>() {
protected void onPreExecute(){
}
@override
protected String doInBackground(String... params) {
Bundle data = new Bundle();
data.putString("my_message", latlng);
data.putString("my_action","http://mydomain/send.aspx");
//data.putString("my_action","com.google.android.gcm.demo.app.ECHO_NOW");
String id = Integer.toString(msgId.incrementAndGet());
try {
gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
msg = data.toString();
return msg;
}

@override
protected void onPostExecute(String msg) {
//mDisplay.setText("");
//mDisplay.append(msg + "\n");
}
}.execute(null, null, null);
return null;
}
public void onLocationChanged(Location location) {
sendmsg("lat:" + Double.toString(location.getLatitude()) + ", " + "lng:" + Double.toString(location.getLongitude()));

}

Here's my web app code to accept the post request from the droid app through gcm

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;

public partial class send : System.Web.UI.Page
{
String GoogleAppID = "AIzaSyDfCV27hE0EpKZhJpaM5uL0bAVo07PTO94";
protected void Page_Load(object sender, EventArgs e)
{
string URI = "https://android.googleapis.com/gcm/send";
string message;
DateTime now = DateTime.Now;
string serverTime = now.ToString("MM/dd/yyyy/:H:mm:ss");
string registration_id = "APA91bFv5LgCQge5NhhcOXQJHXW6tqZGruSnGDlkHSviSiQevMFQDn3svnliekoOjiwv2hRLtEU-vHhu7yJvzFI6Fpwd9Ztks7zdCTOe_eM_B3RttWfIxXwdBDEdq-z87BZLHtXi4O-GM54rxiE3pTbpzQjAdqtvgA";
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded;charset=UTF-8";
wc.Headers[HttpRequestHeader.Authorization] = "key=AIzaSyDfCV27hE0EpKZhJpaM5uL0bAVo07PTO94";
if ((Request["my_message"] != null))
{
message = Request["my_message"] + "\n" + serverTime;
}
else { message = serverTime; }
try {
string HtmlResult = wc.UploadString(URI, "registration_id=" + registration_id + "message=" + message);
Label1.Text = /*HtmlResult + "\n" + */message;
}
catch (Exception error) { error.Message.ToString(); }
}
}
}

So the web code is looking for a post variable or get variable with a name of my_message, which is what I believe the droid is sending out, but maybe not. The web app never finds this. But if it doesn't, it still should send the server time back. So when i do a manual refresh of the web page, it does fire off the WakefulBroadcastReceiver in my droid app, but when i inspected the bundle in the debugger, there's nothing that the web app was supposed to send, only event, collapse key etc....... The line that is causing problems is message = extras.getString("message"); because it is returning null. I thought my web app would send a parameter named "message" but I don't see it, but it still triggers the Broadcast Receiver, so the web app must be sending something. If I use a parameter that I can see is in the bundle, such as collapse_id, it works fine.


public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
private String message;

@override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
Bundle extras = intent.getExtras();
message = extras.getString("message");
MainActivity.mDisplay.setText("");
MainActivity.mDisplay.setText(message);
}

Just in case you need to see this too, here is the intent service needed for WakefulBroadcastReceiver

public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
static final String TAG = "GCMDemo";

public GcmIntentService() {
super("GcmIntentService");
}

@override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
//Toast.makeText(this, extras.toString(), Toast.LENGTH_SHORT);
//mDisplay.append(intent.getExtras().toString());
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);

if (!extras.isEmpty()) { // has effect of unparcelling Bundle
/*
* Filter messages based on message type. Since it is likely that GCM
* will be extended in the future with new message types, just ignore
* any message types you're not interested in, or that you don't
* recognize.
*/
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " +
extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {

// Post notification of received message.
String message = extras.getString("message");
Log.i("Received Message", "Received Message : " + message);
sendNotification("Received: " + message);
/*sendNotification("Received: " + extras.toString());
Log.i(TAG, " ServiceReceived: " + extras.toString());*/
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}

// Put the message into a notification and post it.
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
//MainActivity.mDisplay.append(msg);
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones