2012-12-28 48 views
2

我試圖在我的應用中實施Google雲消息傳遞。但我仍然無法弄清楚爲什麼我沒有把正確的信息傳給我的手機。我的服務器發送消息,GCM服務器響應到併發送一條信息給我phone.This消息看起來像這樣GCM服務器返回空值

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

我想這意味着我得到一個消息,問題是我唯一的應用展示空值。我現在正在使用瀏覽器的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)); 

有誰知道這個問題可能是什麼?

+0

正是你在哪裏顯示,空值? –

+0

將null值返回給模擬器中的android應用程序 – user1732457

+0

您是否認爲將信息包含爲顯示問題的代碼或異常的logcat跟蹤會很重要? –

回答

0

調整你的代碼

'data' => $message, 

是這樣的:

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

和你的onMessage()在GCMIntentService方法應該是象下面這樣:

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

    } 
+0

這似乎沒有幫助,仍然返回空值。 – user1732457

+0

確保您正在捕獲onMessage()中的意圖,而不是您的onReceive()方法。 – selsine

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."); 
}