Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase FCM push, getting Error Missing Registration | Android

I have seen this topic discussed before, but I think it's not the same scenario.

I'm trying to send push notification from one device (going to be an admin device) through FCM (Firebase Cloud Messaging) to all other devices, and i'm going exactly by their docs.

I have tried to subscribe to topics or keep it simple still getting the same error:

MissingRegistration

 String jsonData = "{\"to\":\"/topics/news\",\"notification\":{\"title\":\"title\",\"body\":\"text\" }}";
                byte[] postData = jsonData.getBytes(Charset.forName("UTF-8"));
                int postDataLength = postData.length;

                URL url = new URL("https://fcm.googleapis.com/fcm/send");
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setDoInput(true);
                con.setDoOutput(true);
                con.setInstanceFollowRedirects(true);
                con.setRequestMethod("POST");

                con.setRequestProperty("Content-Type","application/json");
                con.setRequestProperty("Authorization","key=AIzaSyB70J***-z34q2_h*******qjZsM5zBIf8Y"); //I've added stars :-) 
                con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                con.setRequestProperty("Content-Type","charset=UTF-8");
                con.setRequestProperty("Content-Length",Integer.toString(postDataLength));

                con.setUseCaches(false);

                DataOutputStream wr = new DataOutputStream(con.getOutputStream());
                wr.write(postData);

                InputStream inputStream= con.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String line = null;
                String outPut = "";
                while (((line = reader.readLine()) != null)){
                    outPut += line;
                }
like image 940
XcodeNOOB Avatar asked Aug 07 '16 12:08

XcodeNOOB


People also ask

How do I find my FCM registration key?

Click the Cloud Messaging tab next to the General tab. The Project credentials section appears. The Project credentials section displays the Firebase Cloud Messaging token, Sender ID, and the Server key. Copy Sender ID.

How do I send FCM push notifications?

For sending FCM notification payload you can use Firebase Cloud Messaging Tool in firebase console. And click on Send your first message. Then enter the Title and body field. If you wish to send it to a particular device then click on Send test message and enter the FCM registration token.


1 Answers

I was using/trying to send FCM Push notification using Postman web API Client app, to FCM URL: https://fcm.googleapis.com/fcm/send

And I had used a wrong Content-Type: application/x-www-form-urlencoded, So I changed it to

Content-Type:  application/json

This is basically required, as we are sending the Push Notification as a JSON Payload.

Cheers!

like image 84
Randika Vishman Avatar answered Oct 03 '22 04:10

Randika Vishman