2014-02-27 88 views
-4

我使用GCM創建了一個android應用程序,併成功將消息發送到其他設備。GCM android無法在某些設備上發送消息?

但從另一個設備的相同的應用程序沒有任何反應。爲什麼我的GCM不能從其他設備發送任何消息!?

代碼:如果有什麼不對的代碼

public function send_notification($registatoin_ids, $message) { 
     // include config 
     include_once './config.php'; 

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

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

     // Execute post 
     $result = curl_exec($ch); 
     if ($result === FALSE) { 
      die('Curl failed: ' . curl_error($ch)); 
     } 

     // Close connection 
     curl_close($ch); 
     echo $result; 
    } 

指導我。

+2

php代碼?你有一些麻煩。 –

+0

$ result的回顯會告訴你什麼?這應該有什麼錯誤的答案。 – NickT

+0

$ echo {「multicast_id」:7581595512129243700,「success」:1,「failure」:0,「canonical_ids」:0,「results」:[{「message_id」:「0:1393508903773800%f11e78b0f9fd7ecd」} ]} – ltvie

回答

0

檢查設備上的GCM服務apk可用性。

/** 
* Check the device to make sure it has the Google Play Services APK. If 
* it doesn't, display a dialog that allows users to download the APK from 
* the Google Play Store or enable it in the device's system settings. 
*/ 
private boolean checkPlayServices() { 
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
    if (resultCode != ConnectionResult.SUCCESS) { 
     if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { 
      GooglePlayServicesUtil.getErrorDialog(resultCode, this, 
        PLAY_SERVICES_RESOLUTION_REQUEST).show(); 
     } else { 
      Log.i(TAG, "This device is not supported."); 
      finish(); 
     } 
     return false; 
    } 
    return true; 
} 
0

嘗試將ttl設置爲0並執行卷曲調用。如果你不指定ttl,gcm可以在4周內發送它。你的迴應顯示它的成功,但這並不意味着它已經交付。

{ "collapse_key": "score_update", 
    "time_to_live": 108, 
    "delay_while_idle": true, 
    "data": { 
    "score": "4x8", 
    "time": "15:16.2342" 
    }, 
    "registration_ids":["4", "8", "15", "16", "23", "42"] 
} 

有效載荷與所有字段。使用java標記參考此gcm http server

+0

yes.gcm響應是成功的..我可以將ttl設置爲0並在該代碼中執行curl調用?或者也許你有鏈接或一些教程看起來像我上面的情況? – ltvie

+0

答案更新 –

+0

謝謝..通常,但仍在某些設備的消息不可接收。 – ltvie

相關問題