2013-07-31 103 views
4

我知道有很多帖子都有同樣的問題,但是在讀完所有帖子之後,我找不到問題的原因。GoogleCloudMessaging PHP腳本總是返回invalidregistration

我寫了一個GCM客戶端來註冊我的設備,從我的服務器接收消息。它正在工作,我可以將註冊ID存儲在我的數據庫中。

我的問題是在服務器端。我用在什麼地方找到谷歌搜索的腳本,但我總是收到一個錯誤的結果:

{"multicast_id":7341760174206782539,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]} 

的PHP代碼是(我使用的瀏覽器API,而不是服務器API的教程所講述的,但嘗試這兩個鍵的回報相同的消息):

<?php 
// Replace with real BROWSER API key from Google APIs 
$apiKey = "AIzaSyACln4edhSZW30XcmYpqoMz_gcRCC1iFjY"; 

// Replace with real client registration IDs 
$registrationIDs = array("APA91bEdf1w4dQtsUqPT1jHhWEpvrpxzB1yrpL3RVtKrVxfzxfg2-Yl-pwHorsnmSnkqywQ8G90YcGEBoqCjgQU8CnjA0N7mOWF8bHMhHAs4ty46PPTX8yh6eSaSqvU3JTMmb-P0ma90EBG0rsQQbUh3aX895KxitI3LCiGOYqRfE5pZQ"); 

// Message to be sent 
$message = "this is a test"; 

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

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

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

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

// Execute post 
$result = curl_exec($ch); 

// Close connection 
curl_close($ch); 

echo $result; 
?> 

我把真正的ID和密鑰,看看我是否做得對。有任何想法嗎?

回答

0

只要用這個替換代碼就行了。 請確保您替換YOUR_REGISTRATION_ID,YOUR_MESSAGE, YOUR_API_KEY, REGISTRATION_IDS和其他評論與原件。

<?php 
$regId=YOUR_REGISTRATION_ID; 
$msg=YOU_MESSAGE; 
$message = array("message" => $msg); 
$regArray[]=REGISTRATION_IDS; 
$url = 'https://android.googleapis.com/gcm/send'; 

$fields = array('registration_ids' => $regArray, 'data' => $message,); 
$headers = array('Authorization: key=YOUR_API_KEY','Content-Type: application/json'); 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

$result=curl_exec($ch); 
echo $result; 
if($result==FALSE) 
{ 
    die('Curl Failed'); 
} 
curl_close($ch); 
} 
?> 
+0

謝謝你的回答,但我得到一個錯誤:「registration_ids」字段不是JSON數組。 –

+0

更改行$ regArray = REGISTRATION_IDS;到$ regArray =數組(REGISTRATION_IDS),我得到了舊的結果:invalidRegistration; –

+0

嘿,只需將'$ regArray = REGISTRATION_ID'替換爲'$ regArray [] = REGISTRATION_ID' –

0

您確定您要發送給Google的註冊ID的值是正確的嗎?

Invalid Registration ID

Check the formatting of the registration ID that you pass to the server. Make sure it matches the registration ID the phone receives in the com.google.android.c2dm.intent.REGISTRATION intent and that you're not truncating it or adding additional characters. Happens when error code is InvalidRegistration.

雖然我不知道什麼是有效的註冊ID的格式不,我從來沒有見過一個註冊ID以兩個破折號之間的2個字符段,你在你的註冊ID有:...2-Yl-p...

+0

我收到註冊ID,正在監聽com.google.android.c2dm.intent.REGISTRATION意圖操作。所以我認爲這是正確的。比較logcat和我的數據庫註冊ID是糾正。可能是一些本地化問題?我來自巴西並使用葡萄牙語設備進行測試。我會稍後嘗試更改語言。 –

3

我發現我的問題。我的PHP庫從文章中刪除了一些字符或獲取變量。其中一個特徵是'_',因爲它可以用作SQL注入。那是我的問題。固定!

2

我得到了同樣的問題。我的問題是我的gcm_regid。 gcm_regid的長度是183字節。但是,我的數據庫使用150個字節來存儲此值。當然,存儲的gcm_regid是不完整的。我通過增加密鑰的存儲大小來解決問題。然後問題解決了。

8

幾分鐘前我有同樣的問題。

更改此

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

這樣:

$fields = array(
      'registration_ids' => array($registrationIDs), 
      'data'    => array("message" => $message), 
      ); 
+0

數組($ registrationIDs),我有同樣的問題和相同的消息,並且此行也不工作 – delive

+0

($ row _ ['GCM']),工作,你需要迭代在foreach中,我不知道確切,但工程。 – delive

+0

您的解決方案可行!謝謝 :) –

0

後的數組數據,它..

例如:

$registatoin_ids = array($regId); 
$message = array(
      "msg_category"  => "cat001", 
      "msg_identifier" => "msg01", 
      "user_id"   => "1234", 
      "title"    => $title, 
      ); 

,然後調用該函數。 。

$result = $gcm->send_notification($registatoin_ids, $message); 

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); 
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 

    // 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; 
} 
0

類似的問題,並且在結束它是不好的註冊ID(令牌)接收。

添加以下線

echo "id=[$id]"; 

到PHP代碼,以驗證該ID是完全一樣的logcat的印刷令牌並發現了,通過使用上述回波線,即有添加了一個空間到id的開頭和結尾。一旦我刪除了空格,郵件就沒有發生錯誤。

0

您可以使用此代碼:

<?php 

// API access key from Google API's Console 
define('API_ACCESS_KEY', 'AIzaSyDiQ76wcVW2kcwIUQJasuym1JBXXXXX'); 


$registrationIds = array($_POST['id']); 

// prep the bundle 
$msg = array 
(
    'message' => 'here is a message. message', 
    'title'  => 'This is a title. title', 
    'subtitle' => 'This is a subtitle. subtitle', 
    'tickerText' => 'Ticker text here...Ticker text here...Ticker text here', 
    'vibrate' => 1, 
    'sound'  => 1, 
    'largeIcon' => 'large_icon', 
    'smallIcon' => 'small_icon' 
); 

$fields = array 
(
    'registration_ids' => $registrationIds, 
    'notification'   => $msg 
); 

$headers = array 
(
    'Authorization: key=' . API_ACCESS_KEY, 
    'Content-Type: application/json' 
); 

$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send'); 
curl_setopt($ch,CURLOPT_POST, true); 
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields)); 
$result = curl_exec($ch); 
curl_close($ch); 

echo $result.$registrationIds[0]; 

如你願意,你可以改變notificationdata