user1732457
user1732457

Reputation: 177

GCM server returns null value

I am trying to implement Google Cloud Messaging in my app. Still i can't figure out why i don't get the correct message to my phone. My server sends a message, GCM servers respond to to that and send a message back to my phone.This message looks like this

{\"multicast_id\":8186678237008516542,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1356727074650189%12aaaeccf9fd7ecd\"}]}"

I think that means that i get a message, the problem is my app only shows null value. I am using the Browser Api key right now and get these results, but I have tried to use server key(which theoretically is more suitable to my needs), but i get Error 401.

For receiving the message, i use a broadcast receiver

  public void onReceive(Context context, Intent intent){

String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);}

EXTRA_MESSAGE = message

This is the code i use in my server.

$fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message,
    );

    $headers = array(
        'Authorization: key=' . GOOGLE_API_KEY,
        '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);

    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

Does anybody know what the problem might be??

Upvotes: 2

Views: 1169

Answers (2)

Bhunnu Baba
Bhunnu Baba

Reputation: 1802

I think your response string name mismatch, So please check your response string name. I have used it as "price" in my server side code and in my android side code.you can see it in below images.

File at server side : send_message.php

enter image description here

File at application side : GCMNotificationIntentService

public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

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

public static final String TAG = "GCMNotificationIntentService";

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {
        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());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                .equals(messageType)) {

            for (int i = 0; i < 3; i++) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                }

            }
            sendNotification("Message Received from Google GCM Server: "
                    + extras.get("price"));
        }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String msg) {
    //Log.d(TAG, "Preparing to send notification...: " + 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.gcm_cloud)
            .setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    //Log.d(TAG, "Notification sent successfully.");
}

Upvotes: 1

Husam A. Al-ahmadi
Husam A. Al-ahmadi

Reputation: 2056

adjust your code from

'data' => $message,

to be something like this:

'&data.message=' => $message,

and your onMessage() method in GCMIntentService should be something like below:

protected void onMessage(Context ctx, Intent intent) {
        // TODO Auto-generated method stub
        String message =intent.getStringExtra("message");;

    } 

Upvotes: 0

Related Questions