Serverside错误' invalidRegistration'使用GCM(Google Cloud Messaging)

时间:2016-05-06 13:49:58

标签: android google-cloud-messaging chrome-gcm

当我运行GCM客户端代码时,我得到错误" invalidRegistration"。我用引用的page进行了检查。但仍然无法获得成功1.由JSON文件提供的API密钥和服务器API密钥,我在配置过程不同时得到它。但对于两者本身,我得到了InvalidRegistarion .`public class 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
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

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

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

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