import UIKit
import UserNotifications
class SLPushNotificationManager: NSObject, UIApplicationDelegate {
class var currentManager : SLPushNotificationManager {
struct Static {
static let instance : SLPushNotificationManager = SLPushNotificationManager()
}
return Static.instance
}
override init() {
super.init()
NSNotificationCenter.defaultCenter().addObserverForName(UserLoggedInNotification, object: nil, queue: NSOperationQueue.mainQueue()) {[unowned self] (notification) -> Void in
self.registerForRemoteNotifications()
}
}
func registerForRemoteNotifications(){
if #available(iOS 10, *) {
let center = UNUserNotificationCenter.currentNotificationCenter()
center.requestAuthorizationWithOptions([.Alert, .Badge, .Sound], completionHandler: { (granted, error) in
if error == nil {
UIApplication.sharedApplication().registerForRemoteNotifications()
}
})
}else {
let notificationType: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
let settings = UIUserNotificationSettings(forTypes: notificationType, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}// end else
}// end func
Im AppDelegate:swift iOS 10 kann sich nicht neu für die Push-Benachrichtigung registrieren, was fehlt?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
SLPushNotificationManager.currentManager //init push notification manager
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
application.registerForRemoteNotifications()
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
SLPushNotificationManager.currentManager.handleNotification(userInfo)
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
SLPushNotificationManager.currentManager.registerDeviceToken(deviceToken)
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Error: \(error.localizedDescription)", terminator: "")
}
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void){
completionHandler(.Alert)
}
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler:() -> Void){
print(response.notification.request.content.userInfo)
}
nun zum ersten Mal nach dem Herunterladen der App der Benutzer für APN registrieren! und die Methode: func Anwendung (Anwendung: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
heißt aber, nachdem ich für APN unregistere später dann versuche ich registrieren wieder die Methode: func Anwendung (Anwendung: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
wird nicht erneut aufgerufen ... Irgendwelche Ideen y das passiert ??
Sie können diesem Link in Objective C folgen, aber Sie können eine Idee bekommen http://stackoverflow.com/questions/39572897/ios-10-push-notification-issue-solved –