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í.
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.
- Swift Package Manager: Xcode → File → Add Package Dependencies… → Add Local… → chọn thư mục
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>
Khởi tạo trong AppDelegate
Phải gọi
initializetrongdidFinishLaunchingWithOptions: 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.swiftimport 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.
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"])
Nhận sự kiện thông báo & cuộc gọi
Swiftextension 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
- Chạy app trên máy thật, bật log: thấy
[PushConsole] Đã đăng ký thiết bị …. - 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.
- Tab Gửi thông báo: gửi thử tới thiết bị đó.
- 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á.