2016-09-17 1 views
0
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 ??

+0

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 –

Antwort

2

Warum werden Sie abgemeldet? Haben Sie gelesen, was die Dokumentation darüber sagt:

Sie sollten diese Methode nur in seltenen Fällen nennen, wie wenn eine neue Version der App entfernt Unterstützung für alle Arten von Fern Benachrichtigungen

+0

Ich verstehe das. Aber was ich versuche zu wissen, warum es nicht die Delegiertenmethode nennt! unabhängig davon, warum ich den APN abmelden möchte. Es macht einfach keinen Sinn –

+0

Versuchen Sie, die App zu deinstallieren, warten Sie dann 24 Stunden und versuchen Sie es erneut. – Gruntcakes

+0

Ich befolgte vorübergehend Apple Empfehlungen. Ich habe auch Ihren Vorschlag versucht, aber es hat nicht funktioniert ... –

Verwandte Themen