LoginSignup
219
137

More than 3 years have passed since last update.

iOSシミュレータにプッシュ通知を送ってみる

Last updated at Posted at 2020-02-06

※本記事は一般に公開されている範囲で書いています。スクリーンショットの添付は自粛します

Xcode 11.4 からiOSシミュレータがプッシュ通知のシミュレーションをサポートしますね。

Simulator supports simulating remote push notifications, including background content fetch notifications.
Xcode 11.4 Beta Release Notes

シミュレータでプッシュ通知をシミュレーションする方法は2つありますが、両方とも有効な Apple Push Notification Service payload という以下のようなフォーマットのJSONファイルが必要になります(拡張子は .apns である必要があります)。

test.apns
{
    "aps":{
        "alert":"Test",
        "sound":"default",
        "badge":1
    }
}

方法1:apnsファイルをシミュレータにドラッグアンドドロップする

この場合はJSONのトップレベルに Simulator Target Bundle を追加し、アプリの bundle identifier を含める必要があります。

test.apns
{
    "Simulator Target Bundle": "YOUR_APP_BUNDLE_ID",
    "aps":{
        "alert":"Test",
        "sound":"default",
        "badge":1
    }
}

方法2:コマンドラインから送る

simctl を使う方法もあります。この場合 Simulator Target Bundle は不要です。

$ xcrun simctl push <SIMULATOR_DEVICE_ID> <YOUR_APP_BUNDLE_ID> <APNS_FILE_NAME>

例:

$ xcrun simctl push A0AF405F-FE73-45DA-8D7D-F1FE37821992 com.example.my-app test.apns

SIMULATOR_DEVICE_ID は次のコマンドで調べることができます。

xcrun simctl list

注意点

  • この機能を試すためには、アプリがプッシュ通知に対応しており、かつ、ユーザーが通知を許可している必要があります
  • この機能はあくまで シミュレーション なので、APNSを介したプッシュ通知のテストは別途実施したほうが良いでしょう

参考リンク

219
137
1

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
219
137