0
votes
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

In the app delegate:

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)
}

Now for the first time after downloading the app the user can register for APN! and the methode: func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)

is called but after I unregistere for APN later then I try to register again the method: func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)

is not called again... Any Ideas y this happens??

1
You can follow this link its in Objective C but you can get some idea stackoverflow.com/questions/39572897/…Darshan Mothreja

1 Answers

2
votes

Why are you deregistering? Have you read what the documentation says about this:

You should call this method in rare circumstances only, such as when a new version of the app removes support for all types of remote notifications