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.

SDK miễn phí, mã nguồn mở (MIT) trên GitHub. Tạo tài khoản miễn phí để có App ID.

  1. Thêm SDK vào project

    Swift Package Manager (khuyên dùng): Xcode → File → Add Package Dependencies… → dán URL dưới, chọn Up to Next Major Version từ 1.0.0:

    https://github.com/DaoChiMinh/tingpush-sdk

    CocoaPods: giải nén file tải về vào thư mục gốc của project (tạo ra thư mục TingPushSDK/) rồi:

    Podfile
    # Podfile (giải nén file tải về vào gốc project)
    pod 'TingPush', :path => './TingPushSDK/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 TingPush
    
    @main
    class AppDelegate: UIResponder, UIApplicationDelegate, TingPushDelegate {
    
        func application(_ application: UIApplication,
                         didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            TingPush.shared.delegate = self
            TingPush.shared.initialize(
                appId: "YOUR_APP_ID",
                callConfiguration: TingPushCallConfiguration(localizedName: "MyApp") // bỏ nếu không dùng cuộc gọi
            )
            return true
        }
    
        func application(_ application: UIApplication,
                         didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
            TingPush.shared.didRegisterForRemoteNotifications(deviceToken: deviceToken)
        }
    
        func application(_ application: UIApplication,
                         didFailToRegisterForRemoteNotificationsWithError error: Error) {
            TingPush.shared.didFailToRegisterForRemoteNotifications(error: error)
        }
    
        func application(_ application: UIApplication,
                         didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                         fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
            TingPush.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
    TingPush.shared.login(externalUserId: "user-123")
    
    // Khi đăng xuất
    TingPush.shared.logout()
    
    // Gắn tag để lọc khi gửi
    TingPush.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 (kể cả lúc app đang tắt)
        func tingPush(didOpenNotification notification: TingPushNotification) {
            // điều hướng theo notification.data["screen"]...
        }
    
        // Nhận thông báo khi app đang mở
        func tingPush(didReceiveNotification notification: TingPushNotification) {
            // notification.title, notification.body, notification.data
        }
    
        // Người dùng bấm Nghe → kết nối cuộc gọi thật (WebRTC/SIP/Agora…)
        func tingPush(didAcceptCall call: TingPushCall) {
            // call.id, call.callerName, call.data["room"]
        }
    
        // CallKit đã bật audio: bắt đầu phát/thu âm thanh
        func tingPushDidActivateAudioSession() { }
    
        func tingPush(didDeclineCall call: TingPushCall) { }  // từ chối
        func tingPush(didEndCall call: TingPushCall) { }      // cúp máy
        func tingPush(didMissCall call: TingPushCall) { }     // nhỡ
    }
    
    // Đầu bên kia cúp máy → tắt màn hình cuộc gọi
    TingPush.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 [TingPush] Đã đă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 và API Key. Code mẫu trong bảng điều khiển được điền sẵn thông tin dự án của bạn.