iOS (Swift)

SDK oficial PushHub (Swift Package) con APNs directo y extensión de multimedia y botones.

El SDK oficial PushHub es un Swift Package para iOS 15 o superior. Usa APNs directo y no necesita Firebase. Hace lo siguiente:

  • registra el dispositivo, con cola sin conexión;
  • detecta el entorno (sandbox o production) del perfil de aprovisionamiento;
  • muestra las notificaciones con la app abierta;
  • reporta aperturas y clics;
  • gestiona usuarios, tags y topics.

Incluye además una Notification Service Extension lista para heredar, para imágenes, GIF, audio, video y botones.

Requisitos

  • La llave .p8 o el certificado .p12 subidos en PushHub.
  • En Xcode: Signing & Capabilities > Push Notifications y Background Modes > Remote notifications.

Instalación

File > Add Package Dependencies… y agrega el paquete. Mientras no esté publicado, usa Add Local… con la carpeta sdks/ios. Agrega el producto PushHub a la app.

Uso

import PushHub

final class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
        PushHub.configure(PushHubOptions(appKey: "pk_live_TU_LLAVE_PUBLICA", notificationServiceExtension: true))
        PushHub.onNotificationOpened { n in
            // n.deepLink, n.actionId (botón tocado), n.data (tus datos)
        }
        return true
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        PushHub.didRegisterForRemoteNotifications(deviceToken: deviceToken)
    }
}

// Cuando el usuario entienda para qué:
PushHub.requestPermission { estado in }

PushHub.identify("cliente-123")
PushHub.setTags(["plan": "gold"])
PushHub.subscribe("ofertas")
PushHub.pairTestDevice(code: "483920") { ok in }
PushHub.logout()

En SwiftUI, conecta el AppDelegate con @UIApplicationDelegateAdaptor.

El SDK se instala como delegate de UNUserNotificationCenter y reenvía todo al delegate que hubiera antes. Si prefieres manejarlo tú, usa setsNotificationCenterDelegate: false y llama a PushHub.handleWillPresent(_:) y PushHub.handleResponse(_:) desde el tuyo.

Opciones de PushHubOptions

Opción Por defecto Descripción
appKey — Llave pública pk_live_… o pk_test_…. El SDK rechaza llaves secretas.
appId — Solo si la llave no está limitada a una app.
environment .auto .sandbox o .production para forzar el entorno de APNs.
autoRegister false Pedir el permiso al iniciar.
foregroundDisplay true Mostrar las notificaciones con la app abierta.
notificationServiceExtension false La app incluye la extensión; habilita imágenes y botones.
setsNotificationCenterDelegate true El SDK maneja UNUserNotificationCenter.
debug false Logs en la consola.

Con Firebase Messaging

Si tu app ya usa Firebase en iOS, pasa el token de FCM en lugar del de APNs: PushHub.setFCMToken(token). En PushHub configura iOS vía FCM.

Imágenes, GIF, audio, video y botones

  1. File > New > Target > Notification Service Extension.
  2. Agrega a ese target el producto PushHubNotificationService.
  3. Reemplaza su clase:
import PushHubNotificationService

class NotificationService: PushHubNotificationServiceExtension {}
  1. Inicia el SDK con notificationServiceExtension: true.

PushHub marca mutable-content: 1 cuando hay multimedia o botones.

Lo que el SDK reporta

Evento Cuándo
received La notificación llega con la app abierta.
opened El usuario toca la notificación.
clicked El usuario toca un botón (incluye actionId).
permission_granted / permission_denied Cambia el permiso.

Sin SDK (REST)

Convierte el token a hexadecimal (deviceToken.map { String(format: "%02x", $0) }.joined()) y regístralo con POST /v1/devices, con platform: "ios", environment: "auto" y framework: "native_ios". Al abrir, lee _phid y deepLink del userInfo y reporta opened (o clicked con actionIdentifier) a /v1/events.