Skip to content

Commit

Permalink
Merge pull request #64 from auth0/prepare-user-notifications-fwk
Browse files Browse the repository at this point in the history
Deprecate iOS 9 or older notification code
  • Loading branch information
hzalaz authored Jul 10, 2018
2 parents 57b1a97 + bc768f5 commit 9fb37fd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
6 changes: 4 additions & 2 deletions Guardian.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@
"$(PROJECT_DIR)/Carthage/Build/iOS",
);
INFOPLIST_FILE = GuardianApp/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.auth0.guardian.sample;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -885,6 +885,7 @@
SWIFT_INSTALL_OBJC_HEADER = NO;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = 1;
};
name = Debug;
};
Expand All @@ -902,14 +903,15 @@
"$(PROJECT_DIR)/Carthage/Build/iOS",
);
INFOPLIST_FILE = GuardianApp/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.auth0.guardian.sample;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "32df3887-3767-4b67-84a6-144aaf2ef2f2";
PROVISIONING_PROFILE_SPECIFIER = "match Development com.auth0.guardian.sample";
SWIFT_INSTALL_OBJC_HEADER = NO;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = 1;
};
name = Release;
};
Expand Down
3 changes: 3 additions & 0 deletions Guardian/Guardian.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import Foundation

/// Default URLSession used to send requests to Guardian API.
public let defaultURLSession: URLSession = {
let config = URLSessionConfiguration.default
config.requestCachePolicy = .reloadIgnoringLocalCacheData
Expand Down Expand Up @@ -380,7 +381,9 @@ public func notification(from userInfo: [AnyHashable: Any]) -> Notification? {

- parameter withAcceptTitle: the title for the "Accept" notification action
- parameter rejectTitle: the title for the "Reject" notification action
- important: Deprecated for iOS 10 or later, please use UserNotification framework
*/
@available(iOS, deprecated: 10.0, message: "Use UserNotification framework")
public func categoryForNotification(withAcceptTitle acceptTitle: String, rejectTitle: String) -> UIUserNotificationCategory {
let acceptAction = UIMutableUserNotificationAction()
acceptAction.identifier = acceptActionIdentifier
Expand Down
4 changes: 2 additions & 2 deletions Guardian/Notification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import Foundation

public let AuthenticationCategory = "com.auth0.notification.authentication"
let acceptActionIdentifier: String = "\(AuthenticationCategory).accept"
let rejectActionIdentifier: String = "\(AuthenticationCategory).reject"
public let acceptActionIdentifier: String = "\(AuthenticationCategory).accept"
public let rejectActionIdentifier: String = "\(AuthenticationCategory).reject"

/**
A Guardian Notification contains data about an authentication request.
Expand Down
40 changes: 34 additions & 6 deletions GuardianApp/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import UIKit
import Guardian
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
Expand All @@ -36,12 +37,39 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// Override point for customization after application launch.

// Set up push notifications
let category = Guardian.categoryForNotification(withAcceptTitle: NSLocalizedString("Allow", comment: "Accept Guardian authentication request"),
rejectTitle: NSLocalizedString("Deny", comment: "Reject Guardian authentication request"))
let notificationTypes: UIUserNotificationType = [.badge, .sound]
let pushNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: [category])
application.registerUserNotificationSettings(pushNotificationSettings)
application.registerForRemoteNotifications()
let acceptAction = UNNotificationAction(
identifier: Guardian.acceptActionIdentifier,
title: NSLocalizedString("Allow", comment: "Accept Guardian authentication request"),
options: [.authenticationRequired]
)
let rejectAction = UNNotificationAction(
identifier: Guardian.rejectActionIdentifier,
title: NSLocalizedString("Deny", comment: "Reject Guardian authentication request"),
options: [.destructive, .authenticationRequired]
)

let category = UNNotificationCategory(
identifier: Guardian.AuthenticationCategory,
actions: [acceptAction, rejectAction],
intentIdentifiers: [],
options: [])

UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound]) { granted, error in
guard granted else {
return print("Permission not granted")
}
if let error = error {
return print("failed with error \(error)")
}

UNUserNotificationCenter.current().setNotificationCategories([category])
UNUserNotificationCenter.current().getNotificationSettings() { settings in
guard settings.authorizationStatus == .authorized else {
return print("not authorized to use notifications")
}
DispatchQueue.main.async { application.registerForRemoteNotifications() }
}
}

return true
}
Expand Down

0 comments on commit 9fb37fd

Please sign in to comment.