Tích hợp push notification iOS (Swift, APNs, CallKit)

Swift, iOS 13+. Thông báo qua APNs, cuộc gọi qua PushKit + CallKit. Không cần Firebase.

🍎 iOS

Swift, iOS 13+. Thông báo qua APNs, cuộc gọi qua PushKit + CallKit. Không cần Firebase.

Tải SDK trong bảng điều khiển (mục Hướng dẫn tích hợp) sau khi tạo tài khoản miễn phí.

  1. Thêm SDK vào project

    Giải nén file tải về vào thư mục gốc của project (tạo ra thư mục PushConsoleSDK/), rồi chọn một trong hai cách:

    • Swift Package Manager: Xcode → File → Add Package Dependencies… → Add Local… → chọn thư mục PushConsoleSDK/ios.
    • CocoaPods:
    Podfile
    # Podfile
    pod 'PushConsole', :path => './PushConsoleSDK/ios'

    Yêu cầu iOS 13+. SDK không cần Firebase.

  2. Bật capability trong Xcode

    Target → Signing & Capabilities:

    • Push Notifications
    • Background Modes → tick Remote notifications và Voice over IP (cho cuộc gọi)
    Info.plist
    <!-- Info.plist (Xcode tự thêm khi tick Background Modes) -->
    <key>UIBackgroundModes</key>
    <array>
      <string>remote-notification</string>
      <string>voip</string>
    </array>
    <key>NSMicrophoneUsageDescription</key>
    <string>Dùng micro cho cuộc gọi</string>
  3. Khởi tạo trong AppDelegate

    Phải gọi initialize trong didFinishLaunchingWithOptions: khi có cuộc gọi lúc app đã tắt, iOS mở app chạy ngầm và SDK cần sẵn sàng ngay. App SwiftUI: dùng @UIApplicationDelegateAdaptor(AppDelegate.self).

    AppDelegate.swift
    import UIKit
    import PushConsole
    
    @main
    class AppDelegate: UIResponder, UIApplicationDelegate, PushConsoleDelegate {
    
        func application(_ application: UIApplication,
                         didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            PushConsole.shared.delegate = self
            PushConsole.shared.initialize(
                appId: "YOUR_APP_ID",
                serverURL: URL(string: "https://tingpush.com")!,
                callConfiguration: PushConsoleCallConfiguration(localizedName: "MyApp") // bỏ nếu không dùng cuộc gọi
            )
            return true
        }
    
        func application(_ application: UIApplication,
                         didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
            PushConsole.shared.didRegisterForRemoteNotifications(deviceToken: deviceToken)
        }
    
        func application(_ application: UIApplication,
                         didFailToRegisterForRemoteNotificationsWithError error: Error) {
            PushConsole.shared.didFailToRegisterForRemoteNotifications(error: error)
        }
    
        func application(_ application: UIApplication,
                         didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                         fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
            PushConsole.shared.handleRemoteNotification(userInfo)
            completionHandler(.newData)
        }
    }

    Môi trường APNs tự nhận diện: build debug từ Xcode = sandbox, TestFlight/App Store = production.

  4. Gắn user khi đăng nhập

    Swift
    // Sau khi đăng nhập — backend gửi theo External ID này
    PushConsole.shared.login(externalUserId: "user-123")
    
    // Khi đăng xuất
    PushConsole.shared.logout()
    
    // Gắn tag để lọc khi gửi
    PushConsole.shared.setTags(["plan": "vip"])
  5. Nhận sự kiện thông báo & cuộc gọi

    Swift
    extension AppDelegate {
        // Người dùng bấm vào thông báo
        func pushConsole(didOpenNotification userInfo: [AnyHashable: Any]) {
            // điều hướng theo userInfo["screen"]...
        }
    
        // Người dùng bấm Nghe → kết nối cuộc gọi thật (WebRTC/SIP/Agora…)
        func pushConsole(didAcceptCall call: PushConsoleCall) {
            // call.id, call.callerName, call.data["room"]
        }
    
        // CallKit đã bật audio: bắt đầu phát/thu âm thanh
        func pushConsoleDidActivateAudioSession() { }
    
        func pushConsole(didDeclineCall call: PushConsoleCall) { }  // từ chối
        func pushConsole(didEndCall call: PushConsoleCall) { }      // cúp máy
        func pushConsole(didMissCall call: PushConsoleCall) { }     // nhỡ
    }
    
    // Đầu bên kia cúp máy → tắt màn hình cuộc gọi
    PushConsole.shared.endCall(callId: call.id)

✅ Kiểm tra sau khi tích hợp

  1. Chạy app trên máy thật, bật log: thấy [PushConsole] Đã đăng ký thiết bị ….
  2. Tab Thiết bị của dự án: thiết bị xuất hiện; máy iOS có badge VoIP nếu dùng cuộc gọi.
  3. Tab Gửi thông báo: gửi thử tới thiết bị đó.
  4. Chọn 📞 Cuộc gọi, gửi thử khi app đang mở, khi đã tắt app và khi màn hình đang khoá.

Sẵn sàng tích hợp?

Tạo tài khoản để nhận App ID, API Key và tải SDK. Code mẫu trong bảng điều khiển được điền sẵn thông tin dự án của bạn.