├ 04

ios client

In your Swift app, ask the user for permission, register with APNs, then convert the returned Data into the lowercase hex string edgepush expects.

// AppDelegate.swift
import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    UNUserNotificationCenter.current().requestAuthorization(
      options: [.alert, .sound, .badge]
    ) { granted, _ in
      guard granted else { return }
      DispatchQueue.main.async {
        application.registerForRemoteNotifications()
      }
    }
    return true
  }

  func application(
    _ application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
  ) {
    // edgepush expects 64 lowercase hex characters
    let token = deviceToken.map { String(format: "%02x", $0) }.joined()
    Task { await registerToken(token) }
  }

  func application(
    _ application: UIApplication,
    didFailToRegisterForRemoteNotificationsWithError error: Error
  ) {
    print("APNs registration failed:", error)
  }
}

─  notes

  • The token is the same between development and production builds, but only api.push.apple.com (production) or api.sandbox.push.apple.com (sandbox) will accept it depending on which provisioning profile your app was built with. Mark the credential in the edgepush dashboard as production or sandbox accordingly.
  • The token can change when the user reinstalls the app, restores from backup, or migrates to a new device. Send fresh tokens to your server whenever didRegisterForRemoteNotifications fires.
  • Don't use Expo's ExponentPushToken format with edgepush. edgepush wants the native APNs token directly. See the React Native section for the migration.