Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import Firebase
- import UserNotifications
- @UIApplicationMain
- class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
- var window: UIWindow?
- func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
- // Override point for customization after application launch.
- FirebaseApp.configure()
- askAndRegisterForPushNotif()
- return true
- }
- func askAndRegisterForPushNotif() {
- let unAuthCenter = UNUserNotificationCenter.current()
- unAuthCenter.requestAuthorization(options: [.alert, .sound, .badge]) {
- (sucess, error) in
- if sucess {
- DispatchQueue.main.async {
- unAuthCenter.delegate = self
- let appDelegate = UIApplication.shared
- appDelegate.registerForRemoteNotifications()
- print("Push granted: \(sucess)")
- }
- }
- if error != nil {
- print(error?.localizedDescription)
- }
- }
- }
- func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
- print("registered")
- }
- func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
- print("received notif")
- completionHandler()
- }
- func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
- let options: UNNotificationPresentationOptions = [.alert, .sound]
- completionHandler(options)
- }
- }
Add Comment
Please, Sign In to add comment