シミュレータではPush通知が利用できないため
シミュレータでPush通知の登録をしようとすると、コンソールに以下のようなメッセージが出力されます
Failed to register: Error Domain=NSCocoaErrorDomain Code=3010 "remote notifications are not supported in the simulator"
UserInfo={NSLocalizedDescription=remote notifications are not supported in the simulator}
Apple Developerサイトにログイン
左側のMembershipを選択して、Membership Detailsに表示されたTeam IDをメモしておく

Apple Developerサイトにログイン
左側のCertificates, IDs & Profilesを選択
表示された画面で左側のKeysからAllを選択、右上の+をクリックしてキーを作成

作成したら、Downloadをクリックしてキーをダウンロード
AuthKey_XXXXXXXXXX.p8という名前でダウンロードされるXXXXXXXXXXの部分がKey ID
| コンポーネント | 説明 |
|---|---|
| UNUserNotificationCenter | 通知関連の機能を管理する |
| UNNotificationRequest | 通知の内容や、配送のトリガーを内包する |
| UNNotificationAction | 通知内で表示されるボタン |
| UNNotificationCategory | 通知の方法や表示されるアクションの設定をまとめたもの |
| UNNotificationSettings | 通知の設定情報を保持 |
| UNMutableNotificationContent | 通知する内容 |
| UNPushNotificationTrigger | APNsから送信された通知に関するトリガー |
アプリケーションTARGETのSigningから、Teamを選択

アプリケーションTARGETのCapabilitiesから、Push NotificationsをONにする

UIApplicationDelegateのコールバックが必要になるためですUserNotificationsフレームワークを利用するので、インポート
import UserNotifications // MARK: 01. import
ユーザに対して、通知の許可依頼を行う
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// MARK: 02. request to user
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
guard granted else { return }
// 〜省略〜
}
return true
}
ユーザが通知を許可していた場合は、APNsへ登録
**「どのデバイスにインストールされたどのアプリか」**を登録する
これにより、APNsは対象を特定して通知を発行できるようになる
この結果として、一意に識別するトークンが返される
// MARK: 02. request to user
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
guard granted else { return }
// MARK: 03. register to APNs
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
APNsへの登録後に呼び出されるコールバックを実装(UIApplicationDelegateのメソッド)
application(_:didRegisterForRemoteNotificationsWithDeviceToken:)
application(_:didFailToRegisterForRemoteNotificationsWithError:)// MARK: - Callback for Remote Notification
extension AppDelegate {
// MARK: 04-1. succeeded to register to APNs
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Data -> Token string
let tokenBytes = deviceToken.map { (byte: UInt8) in String(format: "%02.2hhx", byte) }
print("Device token: \(tokenBytes.joined())")
}
// MARK: failed to register to APNs
func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register to APNs: \(error)")
}
}
アプリを一度実行して通知の受信を許可し、前の手順でログ出力したトークンをメモしておきます
application(_:didRegisterForRemoteNotificationsWithDeviceToken:)内で外部サービスにトークンを登録する処理が必要になりますDevice token: b92bf1a8af26237ad8dfad91312ece9563c8493e2bc2bf01e3bd9fb690d20d37
import UIKit
import UserNotifications // MARK: 01. import
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// MARK: 02. request to user
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
guard granted else { return }
// MARK: 03. register to APNs
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
return true
}
}
// MARK: - Callback for Remote Notification
extension AppDelegate {
// MARK: 04-1. succeeded to register to APNs
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Data -> Token string
let tokenBytes = deviceToken.map { (byte: UInt8) in String(format: "%02.2hhx", byte) }
print("Device token: \(tokenBytes.joined())")
}
// MARK: failed to register to APNs
func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register to APNs: \(error)")
}
}
Push通知の送信準備(PushNotificationsアプリ)

iOSを選択
Authentication -> TOKEN
| 項目 | 設定値 |
|---|---|
| SELECT P8 | ダウンロードしておいたAuthentication Keyを選択 |
| Enter key id | メモしておいたKey IDを入力 |
| Enter team id | メモしておいたTeam IDを入力 |
Body
| 項目 | 設定値 |
|---|---|
| Enter bundle id | アプリのBundle Identifierを入力 |
| Enter device token | メモしておいたトークンを入力 |
| Enter message | Push通知のメッセージとなるJSONを入力 |
{
"aps": {
"alert": "Minimal message",
"sound": "default"
}
}
Environment
Push通知の送信
Push通知の受信確認
