APNSUtilAppExtension 1.6.0

APNSUtilAppExtension 1.6.0

pisces 维护。



  • pisces

APNSUtil

Swift CI Status Version License Platform Carthage Compatible

  • APNSUtil让代码简单设置和安裝apple推送通知服务。

特性

  • 简单使用apple推送通知服务
  • 无需为任何iOS版本编写代码
  • 支持链式功能编程

导入

import APNSUtil

使用

主视图控制器实现

import UIKit
import APNSUtil

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        APNSManager.shared
            .setTypes([.sound, .alert, .badge])             // setting user notification types
            .register()                                     // register to use apns
            .subscribe(self) {                             // subscribe to receive apns payload
                // your payload model with generic
                guard let payload: APNSPayload = $0.payload() else {
                    return
                }

                print("subscribe", $0.isInactive, $0.userInfo, payload)

                if $0.isInactive {
                    // TODO: write code to present viewController on inactive
                } else {
                    // TODO: write code to show toast message on active
                }
            }.begin()   // begin to receive apns payload
    }
}

App Delegate 的实现

import UIKit
import UserNotifications
import APNSUtil

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        APNSManager.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
        return true
    }

    // MARK: - Push Notification

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        APNSManager.shared.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
        // <<your function to register device token on your server>>(APNSInstance.shared.tokenString)
    }
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    }

    // MARK: - Push Notification for iOS 9

    func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
        APNSManager.shared.application(application, didRegister: notificationSettings)
    }
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        APNSManager.shared.application(application, didReceiveRemoteNotification: userInfo)
    }

    // MARK: - Public Methods (UIApplicationDelegate - Local Notification)

    func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
        APNSManager.shared.application(application, didReceive: notification)
    }
}

extension AppDelegate: UNUserNotificationCenterDelegate {
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        APNSManager.shared.userNotificationCenter(center, willPresent: notification)
    }
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        APNSManager.shared.userNotificationCenter(center, didReceive: response)
    }
}

实现您的数据包模型

struct APNSPayload: Decodable {
    let aps: APS?

    // Add properties here you need

    struct APS: Decodable {
        let sound: String?
        let alert: Alert?
    }

    struct Alert: Decodable {
        let body: String?
        let title: String?
    }
}

将您的数据包模型作为泛型使用

  APNSManager.shared
      .setTypes([.sound, .alert, .badge])
      .register()
      .subscribe(self) {
          guard let payload: APNSPayload = $0.payload() else {
              return
          }

          // write here to process payload model
      }.begin()

与原始的用户信息一起使用

  APNSManager.shared
      .setTypes([.sound, .alert, .badge])
      .register()
      .subscribe(self) {
          let userInfo = $0.userInfo

          // write here to process userInfo
      }.begin()

安装

CocoaPods

CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它

$ gem install cocoapods

需要 CocoaPods 1.0.0+ 版本来构建 APNSUtil 1.1.5+

要使用 CocoaPods 将 APNSUtil 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'

# Default
target '<Your Target Name>' do
    pod 'APNSUtil', '~> 1.6.0'
end

# for AppExtension
target '<Your Target Name>' do
    pod 'APNSUtil/AppExtension', '~> 1.6.0'
end

然后,运行以下命令

$ pod install

Carthage

Carthage是一个去中心化的依赖管理工具,它为您构建依赖并为您提供二进制框架。

您可以使用下面的命令通过Homebrew安装Carthage

$ brew update
$ brew install carthage

要使用Carthage将Alamofire集成到您的Xcode项目中,请在您的Cartfile中指定它

github "pisces/APNSUtil" ~> 1.6.0

运行carthage update来构建框架,并将构建好的APNSUtil.framework拖入您的Xcode项目中。

需求

最低iOS部署目标9.0以上

作者

Steve Kim,[email protected]

许可

APNSUtil可使用MIT许可证使用。查看LICENSE文件获取更多信息。