swift 在Swift 3中实现APNs处理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 在Swift 3中实现APNs处理相关的知识,希望对你有一定的参考价值。
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)
}
}
}
以上是关于swift 在Swift 3中实现APNs处理的主要内容,如果未能解决你的问题,请参考以下文章
在 Swift 3 中实现 fetchedresultsviewcontroller
如何使用 Swift 3 for iOS 在 ViewController.swift 中实现 UICollectionView