1

I am trying to send push notification to the registered device through Java server by following code:

import java.io.IOException;
import java.util.ArrayList;

import com.google.android.gcm.server.Constants;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;


public class Test {




    private void sendNotification()
    {
           try{
                 Sender sender = new Sender("AIzaSyD9GblH8c9qCBqDhLPKMca3dH8JsY4cgjg");
                 ArrayList<String> devicesList = new ArrayList<String>();
                 devicesList.add("APA91bEtN2c3FYSFPpW-KuNtkWT6B9NJUviKN0L0DCjviJ2BtMyhZLqsFSFKfsySI1s88I0u68xETjvpqhvepffRSgwoTmpRXAv0Fbr0gH8CLhHO5CJC-M8");
                 String data = "hi.......";
                 Message message = new Message.Builder()
                                    .collapseKey("1")
                                    .timeToLive(3)
                                    .delayWhileIdle(true)
                                    .addData("message",
                                            data)
                                    .build();
                MulticastResult result = sender.send(message, devicesList, 1);
                            sender.send(message, devicesList, 1);

                            System.out.println(result.toString());
                            if (result.getResults() != null) {
                                int canonicalRegId = result.getCanonicalIds();
                                if (canonicalRegId != 0) {
                               System.out.println("hi...");
                                }
                            } else {
                                int error = result.getFailure();
                                System.out.println(error);
                            }

            }
           catch(Exception e)
           {
               System.out.println(e);
           }
    }
    public static void main(String ar[])
    {
        new Test().sendNotification();


    }

}

and I am getting following error:

MulticastResult(multicast_id=7574342191790679138,total=1,success=0,failure=1,canonical_ids=0,results: [[ error-code=NotRegistered ]]

Similar question was posted earlier but i didn't find any satisfactory answer there.

aug born
  • 248
  • 1
  • 3
  • 20
  • 1
    Possible duplicate of [GCM 'Error : Not Registered'](http://stackoverflow.com/questions/26718115/gcm-error-not-registered) – yennsarah Dec 16 '15 at 09:25
  • Not Registered generally indicates that the token (device id) that you are using is not valid. You should discard it and generate another one. – Arthur Thompson Dec 16 '15 at 16:16

1 Answers1

2

The Registration Id for the device which you are providing in the devicesList is not registered with the gcm.You first need to register your device with the gcm to obtain a valid RegistrationId.After getting this RegId You will pass it to server then,only your server can push the notification through GCM.

Haseeb Anser
  • 494
  • 1
  • 5
  • 19