Mindbox

[iOS] Самостоятельная настройка пушей

Регистрация устройства в APNS

В файле AppDelegate.swift нужно найти и реализовать функцию registerForRemoteNotifications и вызвать в ней Mindbox.shared.notificationsRequestAuthorization.

SwiftCode
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { UIApplication.shared.registerForRemoteNotifications() registerForRemoteNotifications() ... return true }

Передача статус разрешения в Mindbox

Запрашиваем разрешение у пользователя и передаем в Mindbox

SwiftCode
private func registerForRemoteNotifications() { UNUserNotificationCenter.current().delegate = self DispatchQueue.main.async { UNUserNotificationCenter.current().requestAuthorization(options: [ .alert, .sound, .badge]) { granted, error in print("Permission granted: \(granted)") if let error = error { print("NotificationsRequestAuthorization failed with error: \(error.localizedDescription)") } Mindbox.shared.notificationsRequestAuthorization(granted: granted) } } }

Настройка отображения пушей

Настраиваем пуш уведомления, чтобы они отображались когда приложение в foreground

SwiftCode
func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { completionHandler([.alert, .badge, .sound]) }

Передача токена

Передаем токен в Mindbox, чтобы можно SDK знало куда отправлять пуш уведомление

SwiftCode
func application( _ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Mindbox.shared.apnsTokenUpdate(deviceToken: deviceToken) } }

Пример готового кода

SwiftCode
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { UIApplication.shared.registerForRemoteNotifications() registerForRemoteNotifications() ... return true } private func registerForRemoteNotifications() { UNUserNotificationCenter.current().delegate = self DispatchQueue.main.async { UNUserNotificationCenter.current().requestAuthorization(options: [ .alert, .sound, .badge]) { granted, error in print("Permission granted: \(granted)") if let error = error { print("NotificationsRequestAuthorization failed with error: \(error.localizedDescription)") } Mindbox.shared.notificationsRequestAuthorization(granted: granted) } } } func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { completionHandler([.alert, .badge, .sound]) } func application( _ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Mindbox.shared.apnsTokenUpdate(deviceToken: deviceToken) } }
Last modified on