Help us understand the problem. What is going on with this article?


More than 5 years have passed since last update.

【注意1】ここで紹介しているのはGCM Helper Librariesという旧式のライブラリを使用した場合のものです。現在はGCMを使う場合はGoogle Play Services SDKを使うやり方が一般的ですので新規に実装されるかたはそちらを使ったほうがいいと思います。

【注意2】GCM Helper LibraryはC2DM終了に伴い使えなくなる疑いがあります。
コメントでは中の人がGCM Helper LibraryはGCMを使っているよ(=C2DMではない)とありますがアナウンスと矛盾しているので早めの移行をオススメします。


E/AndroidRuntime(4227): FATAL EXCEPTION: IntentService[GCMIntentService-DynamicSenderIds-2]
E/AndroidRuntime(4227): java.lang.IllegalStateException: sender id not set on constructor
E/AndroidRuntime(4227):     at
E/AndroidRuntime(4227):     at
E/AndroidRuntime(4227):     at$ServiceHandler.handleMessage(
E/AndroidRuntime(4227):     at android.os.Handler.dispatchMessage(
E/AndroidRuntime(4227):     at android.os.Looper.loop(
E/AndroidRuntime(4227):     at


     * 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);


public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {

        SharedPreferences prefs = getSharedPreferences("push", Context.MODE_PRIVATE);
        prefs.edit().putString("senderId", "ここにSender ID").commit();
public class PushIntentService extends GCMBaseIntentService {
    protected String[] getSenderIds(Context context) {
        SharedPreferences prefs = context.getSharedPreferences("push", Context.MODE_PRIVATE);
        String senderId = prefs.getString("senderId", null);
        if (senderId == null) {
            throw new IllegalStateException("senderId is not set in the SharedPreferences");
        return new String[]{ senderId };
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away