GCM-一个注册id有效,但多个注册id失败



我想向几个注册的设备发送GCM消息。当只发送到一个(作为测试)时,它是有效的,但当添加其余的id时,我会收到无效注册错误,并且什么都不发送。

我做以下事情:

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

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

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

这对于1个id("regid1")和多个id("gcmid1","gcmid2")都失败。

应该如何格式化注册ID才能正常工作?

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

尝试使用爆炸

$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

最新更新