2013-07-31 98 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