Created
March 8, 2017 02:30
-
-
Save matsuda/43ed73191f824345e2bfaaaa8ad15063 to your computer and use it in GitHub Desktop.
Implement APNs handling in Swift 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UserNotifications | |
import MuddlerKit | |
// MARK: - APNs utilities | |
extension AppDelegate { | |
func applicationWillRegisterForRemoteNotifications_compatible(_ application: UIApplication) { | |
if #available(iOS 10.0, *) { | |
applicationWillRegisterForRemoteNotifications(application) | |
} else { | |
applicationWillRegisterForRemoteNotifications_backward(application) | |
} | |
} | |
func applicationWillRegisterForRemoteNotifications_backward(_ application: UIApplication) { | |
let types: UIUserNotificationType = [.badge, .alert, .sound] | |
let settings = UIUserNotificationSettings(types: types, categories: nil) | |
application.registerUserNotificationSettings(settings) | |
} | |
func clearAllNotifications() { | |
UIApplication.shared.removeAllNotifications() | |
/* | |
if #available(iOS 10.0, *) { | |
UNUserNotificationCenter.current().removeAllPushNotifications() | |
} | |
*/ | |
} | |
func handleRemoteNotification(_ userInfo: [AnyHashable : Any]) { | |
} | |
} | |
@available(iOS 10.0, *) | |
extension AppDelegate { | |
func applicationWillRegisterForRemoteNotifications(_ application: UIApplication) { | |
UNUserNotificationCenter.current().delegate = self | |
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in | |
guard error == nil else { | |
print("error >>> \(error)") | |
return | |
} | |
print("granted >>> \(granted)") | |
if granted { | |
application.registerForRemoteNotifications() | |
} | |
} | |
} | |
} | |
// MARK: - User attention | |
extension AppDelegate { | |
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) { | |
NSLog("notificationSettings >>> %@", notificationSettings) | |
let types = notificationSettings.types | |
if types.isEmpty { | |
print("Not allowed") | |
return | |
} | |
application.registerForRemoteNotifications() | |
} | |
} | |
// MARK: - DeviceToken | |
extension AppDelegate { | |
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { | |
#if DEBUG || ADHOC | |
NSLog("deviceToken [Data] >>> %@", String(format: "%@", deviceToken as CVarArg)) | |
if let token = String(deviceToken: deviceToken) { // MuddlerKit | |
NSLog("deviceToken [String] >>> %@", token) | |
DispatchQueue.global(qos: .default).async { | |
do { | |
let dir = NSTemporaryDirectory() | |
let path = (dir as NSString).appendingPathComponent("devicetoken.txt") | |
try token.write(toFile: path, atomically: true, encoding: .utf8) | |
} catch {} | |
} | |
} | |
#endif | |
} | |
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { | |
#if DEBUG || ADHOC | |
let nsError = error as NSError | |
#if (arch(arm) || arch(arm64)) && os(iOS) | |
NSLog("error >>> %@", nsError) | |
#endif | |
// stored directory with simulator | |
#if (arch(i386) || arch(x86_64)) && os(iOS) | |
if nsError.domain == NSCocoaErrorDomain && nsError.code == 3010 { | |
let token = "iostestdummydevicetoken" | |
if let deviceToken = token.data(using: .utf8) { | |
self.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken) | |
return | |
} | |
} | |
#endif | |
#endif | |
} | |
} | |
// MARK: - Receive RemoteNotification | |
extension AppDelegate { | |
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { | |
print("userInfo >>> \(userInfo)") | |
self.application(application, didReceiveRemoteNotification: userInfo) { (_) in } | |
} | |
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { | |
print("userInfo >>> \(userInfo)") | |
print("applicationState >>> \(application.applicationState.rawValue)") | |
switch application.applicationState { | |
case .active: | |
break | |
default: | |
break | |
} | |
handleRemoteNotification(userInfo) | |
completionHandler(.noData) | |
} | |
} | |
// MARK: - UNUserNotificationCenterDelegate | |
@available(iOS 10.0, *) | |
extension AppDelegate: UNUserNotificationCenterDelegate { | |
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { | |
print("notification >>> \(notification)") | |
handleRemoteNotification(notification.request.content.userInfo) | |
completionHandler([]) | |
} | |
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { | |
print("response >>> \(response)") | |
handleRemoteNotification(response.notification.request.content.userInfo) | |
completionHandler() | |
} | |
} | |
// MARK: - UIApplication | |
extension UIApplication { | |
func removeAllNotifications() { | |
// MuddlerKit | |
if applicationIconBadgeNumber > 0 { | |
applicationIconBadgeNumberSafely(-1) | |
applicationIconBadgeNumberSafely(0) | |
} else { | |
applicationIconBadgeNumberSafely(1) | |
applicationIconBadgeNumberSafely(0) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment