import UserNotifications
successivo, passare al direttore del progetto per la vostra destinazione e nella scheda Generale , cerca la sezione Linked Frameworks and Libraries.
Fare clic su + e selezionare UserNotifications.framework:
// iOS 10 support
if #available(iOS 10, *) {
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
application.registerForRemoteNotifications()
}
// iOS 9 support
else if #available(iOS 9, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
// iOS 8 support
else if #available(iOS 8, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
// iOS 7 support
else {
application.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
}
metodi utilizzare la notifica delegato
// Called when APNs has assigned the device a unique token
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Convert token to string
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
print("APNs device token: \(deviceTokenString)")
}
// Called when APNs failed to register the device for push notifications
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
// Print the error to console (you should alert the user that registration failed)
print("APNs registration failed: \(error)")
}
per la ricezione di notifiche push
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
completionHandler(UIBackgroundFetchResult.noData)
}
Impostazione di notifiche push sta consentendo la funzione all'interno Xcode 8 per la tua app. Semplicemente vai all'editor di progetto per il tuo target e poi fai clic sulla scheda Capabilities. Cerca Notifiche push e attiva il valore su SU.
Controlla qui sotto link per maggiori notifica metodi delegato
gestione delle notifiche locali e remoti UIApplicationDelegate - gestione delle notifiche locali e remoti
https://developer.apple.com/reference/uikit/uiapplicationdelegate
ho scritto una guida su come fare proprio questo: https://eladnava.com/send-push-notifications-to-ios-devices-using-xcode-8-and-swift-3/ –
Notifica in Swift 3.0: https://iosdevcenters.blogspot.com/2016/09/usernotifications-framework-push.html –