Skip to main content
Question

The iOS device is not showing in Dashboard of Klaviyo.


Forum|alt.badge.img

for enabling iOS i creating key uploaded on Klaviyo provided all the details to Klaviyo now in code I added the public API key. but still when i install on the device the no people growth seen on the dashboard.

import KlaviyoSwift
import UserNotifications
import UIKit

// MARK: - App Delegate Implementation
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Initialize Klaviyo with your PUBLIC API key
        KlaviyoSDK().initialize(with: "MY PUBLIC KEY")

        // Configure notification center
        let center = UNUserNotificationCenter.current()
        center.delegate = self

        // Request notification permissions
        let options: UNAuthorizationOptions = [.alert, .sound, .badge]
        center.requestAuthorization(options: options) { granted, error in
            if let error = error {
                print("Authorization error: \(error.localizedDescription)")
                return
            }

            // Always register for remote notifications regardless of authorization status
            // This ensures Klaviyo gets the latest authorization status
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
        }

        return true
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // Convert token to string for debugging
        let tokenString = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
        print("APNs Device Token: \(tokenString)")

        // Register token with Klaviyo
        KlaviyoSDK().set(pushToken: deviceToken)

        print("User properties set successfully.")
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Failed to register for remote notifications: \(error.localizedDescription)")
    }

    // MARK: - Notification Handling
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        // Handle Klaviyo notifications and deep links
        let handled = KlaviyoSDK().handle(
            notificationResponse: response,
            withCompletionHandler: completionHandler
        ) { url in
            print("Handling deep link: \(url.absoluteString)")
            // Add deep link navigation logic here
        }

        if !handled {
            completionHandler()
        }
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        // Show notifications even when app is in foreground
        if #available(iOS 14.0, *) {
            completionHandler([.list, .banner, .sound])
        } else {
            completionHandler([.alert, .sound])
        }
    }
}

Please Guide me is there any issues in my code

Did this topic or the replies in the thread help you find an answer to your question?

0 replies

Be the first to reply!

Reply