Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCM FC / sender id not set on constructor

I have gotten some strange StackTraces from users of my app recently:

Android Version: 2.3.5
Phone Model: GT-I9001
Stacktrace:
java.lang.IllegalStateException: sender id not set on constructor
at com.google.android.gcm.GCMBaseIntentService.getSenderIds(GCMBaseIntentService.java:125)
at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:237)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.os.HandlerThread.run(HandlerThread.java:60)

I am using Rev. 3 of the GCM lib and regarding to the documentation the senderID is not needed to be passed by the constructor anymore ( was that way in C2DM times ) - also this does not crash on my devices and on the devices of a lot of other users. Can somebody shed a light on what is happening on these devices and idealy has some workaround? A non working GCM for these users would be an option for me as device push is optional - but I do not want it to crash ..

Edit here is the source used: https://github.com/ligi/gobandroid/blob/master/src/org/ligi/gobandroid_hd/GCMIntentService.java

like image 572
ligi Avatar asked Aug 16 '12 15:08

ligi


2 Answers

Did you override the method getSenderIds(Context context) from GCMBaseIntentService? From the source code, it mentioned if you do not pass in a SenderID in the constructor, then you need to override getSenderIds(Context context) to provide a SenderID.

Here's the comment from the constructor:

/**
 * Constructor that does not set a sender id, useful when the sender id
 * is context-specific.
 * <p>
 * When using this constructor, the subclass <strong>must</strong>
 * override {@link #getSenderIds(Context)}, otherwise methods such as
 * {@link #onHandleIntent(Intent)} will throw an
 * {@link IllegalStateException} on runtime.
 */
protected GCMBaseIntentService() {
    this(getName("DynamicSenderIds"), null);
}

And the comment for getSenderIds():

/**
 * Gets the sender ids.
 *
 * <p>By default, it returns the sender ids passed in the constructor, but
 * it could be overridden to provide a dynamic sender id.
 *
 * @throws IllegalStateException if sender id was not set on constructor.
 */
protected String[] getSenderIds(Context context) {
    if (mSenderIds == null) {
        throw new IllegalStateException("sender id not set on constructor");
    }
    return mSenderIds;
}
like image 160
azgolfer Avatar answered Oct 25 '22 16:10

azgolfer


Quoting from a Google Group response:

It looks like you're using the default constructor without overriding the getSenderIds() method. As the constructor's javadoc explains:

Constructor that does not set a sender id, useful when the sender id is context-specific. When using this constructor, the subclass must override getSenderIds(Context), otherwise methods such as onHandleIntent(Intent) will throw an IllegalStateException on runtime

If you don't need a dynamic sender id, you should use the constructor that takes the sender id instead.

UPDATE: I think I solved.

Look inside the GCM examples, you have to implemnt this if you use the supert constructor with static YOUR_GCM_SENDER_ID (

public GCMIntentService() {
        super(YOUR_GCM_SENDER_ID);
}

Otherwise, if you use the super constructor without params, you have to override getSenderIds(Context)

Everything it's explained in JavaDoc documentation

UPDATE: Explaining what is YOUR_GCM_SENDER_ID

When you configurd your Google API Project on Google API Console page you have to create your own project and enable GCM API on it.

You project url will be something like

https://code.google.com/apis/console/#project:4815162342

The value after #project: (4815162342 in this example) is your project number, and it will be used later on as the GCM sender ID.

like image 12
StErMi Avatar answered Oct 25 '22 16:10

StErMi