Mustafa Erturk
Mustafa Erturk

Reputation: 73

have a java.lang.IllegalArgumentException exception try to send gcm message ??

I'm getting a java.lang.IllegalArgumentException exception when attempting to send a message to the GCM server from my app server. Registration was successful on the client side.

import java.util.ArrayList;

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

class Notify {
    public static void main(String args[]) {

        try {

            Sender sender = new Sender(
                    "AIzaSyCd9HdSlq51nLzoqxMaFLsxWaoxxIz6-bU");

            ArrayList<String> devicesList = new ArrayList<String>();
            devicesList.add("APA91bFzbdNTf5CVsnSpkPJxMTCUCnjruluaerQcONQq9f20aaHw1CMNcVFgBsnLd6gUQT5TxrV2WY5WBw9U9u4Paj3dAXgRbaATc9a2Gp0voF3h5elX3Be0r3xw3Kun2HZT1m3D8L6IwCkXtTiILIfVasiKd2uN3w");
            devicesList.add("APA91bFzbdNTf5CVsnSpkPJxMTCUCnjruluaerQcONQq9f20aaHw1CMNcVFgBsnLd6gUQT5TxrV2WY5WBw9U9u4Paj3dAXgRbaATc9a2Gp0voF3h5elX3Be0r3xw3Kun2HZT1m3D8L6IwCkXtTiILIfVasiKd2uN3w");

            // use this line to send message with payload data
            Message message = new Message.Builder()
                    .collapseKey("1")
                    .timeToLive(3)
                    .delayWhileIdle(true)
                    .addData("message",
                            "this text will be seen in notification bar!!")
                    .build();

            // Use this for multicast messages
            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) {
                }
            } else {
                int error = result.getFailure();
                System.out.println(error);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

that's the code

java.lang.IllegalArgumentException: argument cannot be null
    at com.google.android.gcm.server.Sender.nonNull(Sender.java:553)
    at com.google.android.gcm.server.Sender.getString(Sender.java:534)
    at com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:365)
    at com.google.android.gcm.server.Sender.send(Sender.java:261)
    at com.example.server.Notify.main(Notify.java:34)

and thats my problem

Upvotes: 1

Views: 4971

Answers (4)

enl8enmentnow
enl8enmentnow

Reputation: 943

The GCM server is returning an error like "401 Unauthorized", but there is a bug in the GCM code where it throws this IllegalArgumentException instead of the intended InvalidRequestException. You need to attach source and then set a breakpoint at at this code in com.google.android.gcm.server.Sender to see what the actual HTTP error is.

int status = conn.getResponseCode();
String responseBody;
if (status != 200) {
  responseBody = getString(conn.getErrorStream());
  logger.finest("JSON error response: " + responseBody);
  throw new InvalidRequestException(status, responseBody);
}

Basically conn.getErrorStream() is returning null here so you never see the InvalidRequestException.

The code above is from the latest 1.0.2 release. There is a related fix in the latest GCM GIT repository, https://code.google.com/p/gcm/issues/detail?id=7, but looking at changes they made to Sender it still seems like it's going to be an issue.

Upvotes: 1

Ray H
Ray H

Reputation: 333

Although the answer above was selected, I believe this is the cause.

https://code.google.com/p/gcm/issues/detail?id=7

The issue is fixed in master, but the jar in the SDK is out of date.

Upvotes: 1

Parvin Gasimzade
Parvin Gasimzade

Reputation: 26012

There is a problem in the reading response part. It should be like :

MulticastResult multiResult = sender.send(message, devicesList, 1);
List<Result> results = multiResult.getResults();

for (int i = 0; i < results.size(); i++) {
    Result result = results.get(i);

    if (result.getMessageId() != null) {
        // Success
        String canonicalRegId = result.getCanonicalRegistrationId();
        if (canonicalRegId != null) { 
            // same device has more than one registration id.Update it
        }
    } else {
        // Error occurred
        String error = result.getErrorCodeName();
    }
}

Upvotes: 1

Yahor10
Yahor10

Reputation: 2132

Problem of your AIzaSyCd9HdSlq51nLzoqxMaFLsxWaoxxIz6-bU - use another one.

Upvotes: 1

Related Questions