Latest 1.3.0
Homepage https://github.com/pisces/APNSUtil
License MIT
Platforms ios 9.0
Authors

APNSUtil

Swift
Swift
CI Status
Version
License
Platform
Carthage Compatible

  • APNSUtil makes code simple settings and landing for apple push notification service.

Features

  • Using apple push notification service simply
  • No need write codes for any iOS versions
  • Support chained functional programing

Import

import APNSUtil

Using

Implementation for main view controller

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
    }
}

Implementation for 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 {
        // Logic for handling push notifications at initial startup
        APNSManager.shared.didFinishLaunching(withOptions: launchOptions)
        return true
    }

    // MARK: - Push Notification

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

    // MARK: - Push Notification for iOS 9

    func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
        application.registerForRemoteNotifications()
    }
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        APNSManager.shared.didReceive(userInfo: userInfo, isInactive: application.applicationState == .inactive)
    }

    // MARK: - Push Notification for iOS 10 or higher

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        APNSManager.shared.didReceive(userInfo: notification.request.content.userInfo, isInactive: false)
    }
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        APNSManager.shared.didReceive(userInfo: response.notification.request.content.userInfo, isInactive: true)
    }
}

Implement your payload model

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?
    }
}

Using with your payload model as generic

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

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

Using with raw userInfo

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

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

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 1.0.0+ is required to build APNSUtil 1.1.5+.

To integrate APNSUtil into your Xcode project using CocoaPods, specify it in your Podfile:

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

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

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

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Alamofire into your Xcode project using Carthage, specify it in your Cartfile:

github "pisces/APNSUtil" ~> 1.3.0

Run carthage update to build the framework and drag the built APNSUtil.framework into your Xcode project.

Requirements

iOS Deployment Target 9.0 higher

Author

Steve Kim, [email protected]

License

APNSUtil is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "APNSUtilAppExtension",
    "version": "1.3.0",
    "summary": "APNSUtil is makes code simple using apple push notification service.",
    "description": "APNSUtil is makes code simple using apple push notification service.",
    "homepage": "https://github.com/pisces/APNSUtil",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "pisces": "[email protected]"
    },
    "source": {
        "git": "https://github.com/pisces/APNSUtil.git",
        "tag": "1.3.0"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source_files": "APNSUtil/Classes/**/*",
    "pod_target_xcconfig": {
        "OTHER_SWIFT_FLAGS": "-DAPP_EXTENSIONS"
    }
}

Pin It on Pinterest

Share This