Created
January 1, 2020 10:35
-
-
Save G-Vishal/276cbcc980cefb60371bc886a1c6ac25 to your computer and use it in GitHub Desktop.
This code Will check the notification Setting from any View Controller and redirect to the Setting Page to Enable Notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NotificaitonCheck.swift | |
// | |
// Created by Vishal Shelake on 01/01/20. | |
// Copyright © 2020 Solace. All rights reserved. | |
// | |
import Foundation | |
import UserNotifications | |
class NotificaionStatusCheck { | |
var window: UIWindow? | |
private var currentViewController : UIViewController? = nil | |
static let shared = NotificaionStatusCheck() | |
public func currentViewController(_ vc: UIViewController?) { | |
self.currentViewController = vc | |
checkNotificationsAuthorizationStatus() | |
} | |
private func checkNotificationsAuthorizationStatus() { | |
let userNotificationCenter = UNUserNotificationCenter.current() | |
userNotificationCenter.getNotificationSettings { (notificationSettings) in | |
switch notificationSettings.authorizationStatus { | |
case .authorized: | |
print("The app is authorized to schedule or receive notifications.") | |
case .denied: | |
print("The app isn't authorized to schedule or receive notifications.") | |
self.NotificationPopup() | |
case .notDetermined: | |
print("The user hasn't yet made a choice about whether the app is allowed to schedule notifications.") | |
self.NotificationPopup() | |
case .provisional: | |
print("The application is provisionally authorized to post noninterruptive user notifications.") | |
self.NotificationPopup() | |
} | |
} | |
} | |
private func NotificationPopup(){ | |
let alertController = UIAlertController(title: "Notification Alert", message: "Please Turn on the Notification to get update every time the Show Starts", preferredStyle: .alert) | |
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in | |
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else { | |
return | |
} | |
if UIApplication.shared.canOpenURL(settingsUrl) { | |
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in | |
}) | |
} | |
} | |
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil) | |
alertController.addAction(cancelAction) | |
alertController.addAction(settingsAction) | |
DispatchQueue.main.async { | |
self.currentViewController?.present(alertController, animated: true, completion: nil) | |
} | |
} | |
} | |
//to use this Function call in the any viewController | |
//NotificaionStatusCheck.shared.currentViewController(self) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment