Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
- // Ask user for Notification permission
- registerNotification() {(allowed) -> Void in
- if allowed {
- UIApplication.shared.registerForRemoteNotifications()
- }
- }
- return true
- }
- func registerNotification(completion: @escaping (_ allowed: Bool) -> Void) {
- UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
- (granted, error) in
- print("Permission granted: \(granted)")
- // Get setting to check answer from User
- UNUserNotificationCenter.current().getNotificationSettings { (settings) in
- print("Notification settings: \(settings)")
- guard settings.authorizationStatus == .authorized else {
- completion(false)
- return
- }
- }
- completion(true)
- }
- }
- func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
- let tokenParts = deviceToken.map { data -> String in
- return String(format: "%02.2hhx", data)
- }
- let token = tokenParts.joined()
- print("Device Token: \(token)")
- }
- func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
- print("Failed to register: \(error)")
- }
Add Comment
Please, Sign In to add comment