我怎样才能从 GCM 获得正确的响应,以使用 php 删除无效的注册 ID


How can I get the right response from GCM to delete invalid registrationID's with php?

我希望有人能帮助我!对不起我的英语;)我有以下PHP代码:

    //Android - Push Notification Variables
    $apiKey = '';
    //Android - Push Notification Service
    $registrationIDs = array();
    $abfrage = "SELECT id, registrationId, active FROM `push-android` WHERE active = 'true'";
    $ergebnis = mysql_query($abfrage) or die(mysql_error());
    while($row = mysql_fetch_object($ergebnis))
    {
        $id = $row->id;
        $registrationIDs[] = $row->registrationId;
    }
    $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);
    $obj = json_decode($result);
    $return = $obj->{'failure'};
    print_r($obj);
?>

那么,我能做些什么,从GCM获得正确的响应,从我的数据库中删除无效的注册ID?我已经在我的Android手机上安装了该应用程序,发送了通知,什么工作正常。我卸载应用程序后,发送通知,响应显示:

stdClass 对象 ( [multicast_id] => 8.6408433884968E+18 [成功] => 1 [失败] => 0 [canonical_ids] => 0 [结果] => 数组 ( [0] => stdClass Object ( [message_id] => 0:13813433954546524%df6f31cff9fd7ecd ) ) )

卸载

应用程序后,您将无法获得InvalidRegistration。你会得到NotRegistered错误。

但是,GCM 服务器需要一些时间才能确定应用是否已从设备中卸载。这就是为什么您在卸载应用程序后立即发送消息时会从 GCM 获得成功响应的原因。尝试多次发送消息。对我来说,通常在我发送到设备的第二条消息之后(卸载后(,我会收到NotRegistered错误。