Import UserNotifications into your ViewController
import UserNotifications
Create a method to check the current notification status
private func hasNotificationPermission(completion: @escaping (Bool) -> Void) {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
completion(settings.authorizationStatus == .authorized)
}
}
Add observer to detect when app becomes active
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.openactivity), name: NSNotification.UIApplicationDidBecomeActive, object: nil)
}
When coming back from background check for Notification status and update your UI
@objc func appBecomeActive() {
if hasNotificationPermission {
// Notifications are enabled
} else {
// Notifications are not enabled
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…