16
19

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.

シミュレーターでのiOSのプッシュの動作確認にSimulatorRemoteNotificationsが便利!

Last updated at Posted at 2016-11-28

iOSシミュレーターでのプッシュ開発

さて本題ですが、iOS開発において、プッシュの動作確認って面倒なんです。
証明書つくって、デバイストークンをログに出して、Pythonかなんかでスクリプト組んで、実機で確認!みたいな。

なので、適当にやってしまいがち(違)ですが、ちゃんとやろうと思って、いろいろ調べていたら、
いいものを見つけました。

名前のまんまですね!使い方は簡単で、AppDelegate.m

# import <SimulatorRemoteNotifications/UIApplication+SimulatorRemoteNotifications.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
# if DEBUG
    [application listenForRemoteNotifications];
# endif
    return YES;
}

として、あとはターミナルを開いて、

echo -n '{"aps":{"alert":"message", "badge":2, "content-available":1 }, "message":"message"}' | nc -4u -w1 localhost 9930

と打てばオッケー!

このOSSがアプリ内で、localhostの9930ポートにローカルサーバーを立ててくれて、プッシュのシミュレートをしてくれます。

これで、- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandlerが呼ばれるので、あとはよしなに。

アプリ起動中でも、バックグラウンドでも上記メソッドを呼んでくれるのですが、OSが管理しているところのNotification Centerにプッシュ通知を出してくれたり、アプリアイコンにバッジを付けてくれたりまではしないので、そのあたりのタストはどうしても実機になります。

ただ、これでだいぶと作業が捗ると思います。

よくあるやつ

タブバーにバッジを付けたい!みたいなやつ。忘れがちなのは、アプリがバックグラウンドで、プッシュが来て、プッシュから起動せずに、アプリアイコンから起動してしまったケース。

バッジは本当は、起動時にサーバーからとるのが一番よい気がしますが、めんどくさいので、バックグラウンドでプッシュが受け取れた場合のみ表示することにしましょう。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{ 
    // save userInfo in NSUserDefaults
    completionHandler( UIBackgroundFetchResultNoData );
}

みたいに、バックグランドで来た時もNSUserDefaultsかなんかに保存してからcompletionHandlerを呼んでおけば大丈夫ですね。

3年前のstack overflowにも似たような質問があったので、つたない英語で回答(初回答!)

ってな感じで、シミュレーターでのプッシュ動作確認のお話でした!

16
19
2

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
16
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?