GCM - 一个注册ID正在运行,但多个注册ID失败

时间:2015-04-04 11:08:43

标签: android google-cloud-messaging

我想将GCM消息发送到多个注册设备。当仅发送到一个(作为测试)时,它可以工作,但是当添加其余的ID时,我得到invalidregistration错误并且没有发送任何内容。

我执行以下操作:

$sql = "select group_concat(gcm_id) from users where gcm_id is not null";

如果只有1个id(regid1),则它有效,如果有多个(regid1,regid2),则无效注册失败。我尝试了以下方法:

$sql = "select group_concat(concat('\"',gcm_id,'\"')) from users where gcm_id is not null";

对于1 id(“regid1”)和几个id(“gcmid1”,“gcmid2”)都失败了。

如何格式化注册表以使其起作用?

$sql = "select group_concat(gcm_id) from users where gcm_id is not null";
    if ($stmt = $con->prepare($sql)) {
        if ($stmt->execute()) {                             
            $stmt->bind_result($gcm_ids);
            $stmt->fetch();         
            $ids = array($gcm_ids);
            $stmt->close();                         
        } else trigger_error("Problem retrieving gcm ids");
    } else trigger_error("Problem retrieving gcm ids");
    $con->close();      

if (empty($gcm_ids)) trigger_error("no registrations"); 
    else sendGoogleCloudMessage(  $data, $ids );

    function sendGoogleCloudMessage( $data, $gids ) {   
        $apiKey = '...';
        $url = 'https://android.googleapis.com/gcm/send';

        $post = array('registration_ids' => $gids,'data' => $data);

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

        $ch = curl_init();
        // Set URL to GCM endpoint
        curl_setopt( $ch, CURLOPT_URL, $url );
        // Set request method to POST
        curl_setopt( $ch, CURLOPT_POST, true );
        // Set our custom headers
        curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
        // Get the response back as string instead of printing it
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        // Set post data as JSON
        curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
        // Actually send the push!
        $result = curl_exec( $ch );
        if (curl_errno($ch)) trigger_error('Error: '.curl_error($ch));
        else echo "sent: ".$result;
        curl_close( $ch );                  
    }   

1 个答案:

答案 0 :(得分:1)

尝试使用爆炸

$ids = explode(",",$gcm_ids);

registration_ids应该作为数组提交。

{ "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"]
}

可在此处找到更多信息https://developer.android.com/google/gcm/http.html