GCM FC / sender id not set on constructor
Asked Answered
P

2

13

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

Pledge answered 16/8, 2012 at 15:47 Comment(0)
H
18

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;
}
Haerr answered 17/8, 2012 at 15:7 Comment(5)
thanks for your answer, but I have not overwritten this constructor here is the source github.com/ligi/gobandroid/blob/master/src/org/ligi/… also the doc here: developer.android.com/guide/google/gcm/gs.html states nothing about overwriting getSenderIdsPledge
If you have the GCM extras installed, then you will have access to the source code of GCMBaseIntentService.java, take a look at that file and that's what mentioned in the source code.Haerr
I am not yet 100% about this whole thing but I accept and upvote your answer as I am thankful for it!Pledge
@vipsy yea this seems to work - havent got any of those error messages anymorePledge
It is now also mentioned in the GCM docs at the end of step 2 here: developer.android.com/guide/google/gcm/gs.html#android-appScotia
M
12

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.

Mesomorphic answered 20/9, 2012 at 12:56 Comment(1)
Here the "Global.GCM_SENDER_ID" is the value like 2372498294 that @Mesomorphic acquired via his GCM setup with Google, and has put into his app as a constant. The value is the same sender id that is used in GCMRegistrar.register(this, SENDER_ID);Skid

© 2022 - 2024 — McMap. All rights reserved.