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
在app委托中:
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)
}
现在,在下载应用程序后,用户可以首次注册APN!和methode:func应用程序(应用程序:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:NSData)
调用但是在我稍后取消注册APN后,我再次尝试注册方法:func application(application:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:NSData)
是不是再次被召唤......任何想法都会发生这种情况?
1 回答
你为什么要取消注册?您是否阅读过文档中有关此内容的内容: