Guest User

Untitled

a guest
Nov 21st, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import UIKit
  2. import Firebase
  3. import UserNotifications
  4.  
  5. @UIApplicationMain
  6. class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
  7.  
  8. var window: UIWindow?
  9.  
  10.  
  11. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  12. // Override point for customization after application launch.
  13. FirebaseApp.configure()
  14.  
  15. askAndRegisterForPushNotif()
  16. return true
  17. }
  18.  
  19. func askAndRegisterForPushNotif() {
  20. let unAuthCenter = UNUserNotificationCenter.current()
  21. unAuthCenter.requestAuthorization(options: [.alert, .sound, .badge]) {
  22. (sucess, error) in
  23. if sucess {
  24. DispatchQueue.main.async {
  25. unAuthCenter.delegate = self
  26. let appDelegate = UIApplication.shared
  27. appDelegate.registerForRemoteNotifications()
  28. print("Push granted: \(sucess)")
  29. }
  30. }
  31.  
  32. if error != nil {
  33. print(error?.localizedDescription)
  34. }
  35.  
  36. }
  37. }
  38.  
  39. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  40. print("registered")
  41. }
  42.  
  43. func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  44. print("received notif")
  45. completionHandler()
  46. }
  47.  
  48. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  49.  
  50. let options: UNNotificationPresentationOptions = [.alert, .sound]
  51. completionHandler(options)
  52. }
  53.  
  54. }
Add Comment
Please, Sign In to add comment