使用GCM(谷歌云消息传递)'invalidRegistration'服务器端错误



当我运行GCM客户端代码时,我得到的错误是"invalidRegistration"。我查了一下相关页面。但仍然无法获得成功1.JSON文件提供的API密钥和我在配置过程不同时获得的服务器API密钥。但就两者而言,我得到了InvalidRegisterion`公共类MainActivity扩展了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
}

提前谢谢。

云消息-注册客户端应用程序建议,如果使用指数回退注册失败,客户端应用程序应重试注册操作。

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

希望提供的参考资料能有所帮助。:)

相关内容

  • 没有找到相关文章

最新更新