8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GCMBaseIntentServiceを継承したクラスではgetSenderIds()を実装した方がよさげ(もう古いです)

Last updated at Posted at 2013-07-09

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

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

Push登録をGCMBaseIntentService内部でリトライする際に以下のような例外を吐く事がある.

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 com.google.android.gcm.GCMBaseIntentService.getSenderIds(GCMBaseIntentService.java:125)
E/AndroidRuntime(4227): 	at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:237)
E/AndroidRuntime(4227): 	at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
E/AndroidRuntime(4227): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(4227): 	at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(4227): 	at android.os.HandlerThread.run(HandlerThread.java:60)

Googleのチュートリアルを参考に作っていたのでちゃんとjavadocを見てなかったんだけどコンストラクタの説明にちゃんと記述されていました。

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

引数無しのコンストラクタを使う場合はgetSenderIdsをoverrideしないとIllegalStateExceptionが飛ぶって事ですね。

という訳でContextが引数で渡されているのでSharedPreferencesでsenderIdを受け渡しする形に修正

MainActivity.java
public class MainActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		SharedPreferences prefs = getSharedPreferences("push", Context.MODE_PRIVATE);
		prefs.edit().putString("senderId", "ここにSender ID").commit();
	}
}
PushIntentService.java
public class PushIntentService extends GCMBaseIntentService {
	@Override
	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 };
	}
}
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?