GCM 的服务器端错误 'invalidRegistration'(Google 云消息传递)

Serverside error 'invalidRegistration' with GCM (Google Cloud Messaging)

当我 运行 GCM 客户端代码时,出现 "invalidRegistration" 错误。我检查了引用的 page。但仍然无法获得成功,因为 1.The API 密钥由 JSON 文件和服务器 API 密钥提供,我在什么时候得到它配置过程本身都是 different.But,我得到了 InvalidRegistarion。`public class MainActivity extends AppCompatActivity {

private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextView = (TextView) findViewById(R.id.textView);

    new GCMRequest().execute();
}


private class GCMRequest extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... voids) {

        final String API_KEY = "AIzaSyCNPlrqXeZ8IlNYsQGlseHhHPCfgcR5V7c"; // An API key saved on the app server that gives the app server authorized access to Google services
        final String CLIENT_REG_ID = "821083769456"; //An ID issued by the GCM connection servers to the client app that allows it to receive messages
        final String postData = "{ \"registration_ids\": [ \"" + CLIENT_REG_ID + "\" ], " +
                "\"delay_while_idle\": true, " +
                "\"data\": {\"tickerText\":\"My Ticket\", " +
                "\"contentTitle\":\"My Title\", " +
                "\"message\": \"Test GCM message from GCMServer-Android\"}}";

        try {
            URL url = new URL("https://android.googleapis.com/gcm/send");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.setRequestProperty("Authorization", "key=" + API_KEY);

            OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream());
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
            writer.write(postData);
            writer.flush();
            writer.close();
            outputStream.close();

            int responseCode = urlConnection.getResponseCode();
             InputStream inputStream;
            if (responseCode < HttpURLConnection.HTTP_BAD_REQUEST) {
                inputStream = urlConnection.getInputStream();
            } else {
                inputStream = urlConnection.getErrorStream();
            }
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String temp, response = "";
            while ((temp = bufferedReader.readLine()) != null) {
                response += temp;
            }
            return response;
        } catch (IOException e) {
            e.printStackTrace();
            return e.toString();
        }
    }

    @Override
    protected void onPostExecute(String message) {
        super.onPostExecute(message);

        if (mTextView != null) {
            try {
                JSONObject jsonObject = new JSONObject(message);
                mTextView.setText(jsonObject.toString(5));
            } catch (JSONException e) {
                e.printStackTrace();
                mTextView.setText(e.toString());
            }
        }
    }
}

}`

输出是:

{
multicast_id:XXXX,
success:0,
failure:1,
results:
{
error:InvalidRegistration
}

提前致谢。

Cloud Messaging - Registering Client Apps 建议,如果使用指数退避法注册失败,客户端应用程序应重试注册操作。

除此之外,还提到您应该从服务器启动令牌刷新,以保护客户端应用程序和应用程序服务器免受潜在的恶意重复使用注册令牌。当从服务器端启动 GCM 注册令牌刷新时,客户端应用程序必须使用 GCM 注册 client/server 握手处理 tokenRefreshed 消息。

希望给定的参考资料有所帮助。 :)