GCM服务器返回空值

时间:2012-12-28 21:01:48

标签: php android google-cloud-messaging

我正在尝试在我的应用中实施Google Cloud Messaging。我仍然无法弄清楚为什么我没有收到正确的信息给我的手机。我的服务器发送一条消息,GCM服务器响应该消息并将消息发送回我的手机。此消息如下所示

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

我认为这意味着我收到了一条消息,问题是我的应用只显示空值。我现在正在使用Browser Api键并获得这些结果,但我尝试使用服务器密钥(理论上它更适合我的需要),但我得到错误401.

为了接收消息,我使用广播接收器

  public void onReceive(Context context, Intent intent){

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

EXTRA_MESSAGE =消息

这是我在服务器中使用的代码。

$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));

有人知道问题可能是什么吗?

2 个答案:

答案 0 :(得分:1)

我认为你的响应字符串名称不匹配,所以请检查你的响应字符串名称。我用它作为"价格"在我的服务器端代码和我的android端代码。你可以在下面的图像中看到它。

服务器端的文件:send_message.php

enter image description here

应用程序端的文件: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.");
}

答案 1 :(得分:0)

调整您的代码
'data' => $message,

是这样的:

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

并且GCMIntentService中的onMessage()方法应如下所示:

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

    }