Guest User

Untitled

a guest
Jun 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  2.  
  3. // Ask user for Notification permission
  4. registerNotification() {(allowed) -> Void in
  5. if allowed {
  6. UIApplication.shared.registerForRemoteNotifications()
  7. }
  8. }
  9. return true
  10. }
  11.  
  12. func registerNotification(completion: @escaping (_ allowed: Bool) -> Void) {
  13. UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
  14. (granted, error) in
  15. print("Permission granted: \(granted)")
  16.  
  17. // Get setting to check answer from User
  18. UNUserNotificationCenter.current().getNotificationSettings { (settings) in
  19. print("Notification settings: \(settings)")
  20. guard settings.authorizationStatus == .authorized else {
  21. completion(false)
  22. return
  23. }
  24. }
  25. completion(true)
  26. }
  27. }
  28.  
  29. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  30. let tokenParts = deviceToken.map { data -> String in
  31. return String(format: "%02.2hhx", data)
  32. }
  33. let token = tokenParts.joined()
  34. print("Device Token: \(token)")
  35. }
  36.  
  37. func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  38. print("Failed to register: \(error)")
  39. }
Add Comment
Please, Sign In to add comment