The question is published on by Tutorial Guruji team.
When I run the GCM client-side code,I got error as “invalidRegistration”. I checked with the referedpage.But still unable to get success as 1.The API key provided by JSON file and Server API Key which I got it when the configuration process are different.But for both itself, I got 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()); } } } }
}`
the output is:
{ multicast_id:XXXX, success:0, failure:1, results: { error:InvalidRegistration }
Thanks in advance.
Answer
Recommended from Cloud Messaging – Registering Client Apps that, client app should retry the registration operation if registration fails using exponential back-off.
Aside from that, it was also mentioned that you should initiate token refresh from the server to protect the client app and app server from potential malicious re-use of registration tokens. When GCM registration token refresh is initiated from server side, the client app must handle a tokenRefreshed message with the GCM registration client/server handshake.
Hope the given reference helps. 🙂