https://leedeokho.github.io/2018/03/13/PushKit


목차


PushKit이란

앱을 업데이트(새로운 정보가 있을 때)하기 위해 푸시 알람을 앱에 보내는 것.

PushKit 프레임워크는 특정한 종류(VoIP invitations, watchOS complication updates, and file provider change notifications)의 알림을 앱에 전송한다.

PushKit 프레임워크는 UserNotificatios와 달리 배지, 알림, 소리는 표시되지 않는다.

PushKit notifications는 user notifications에 비해 다음과 같은 이점이 있다.

  • 앱이 실행 중이지 않으면알림을 받는 즉시 시스템이 자동으로 앱을 시작한다. silent user notifications를 사용할 수도 있지만, notification이 왔을 때 앱이 작동되지 않을 수도 있다.
  • 앱이 백그라운드에서 실행 중인 경우에도 알림을 처리하도록 런타임이 제공된다.
  • 장치는 PushKit notification을 받을 때에만 깨어난다. 이로 인해 배터리 수명을 개선할 수 있다.
  • PushKit notifications에는 user notifications보다 더 많은 data가 포함될 수 있다.

PkPushType

내가 원하는 것을 지원할 푸시 타입을 반영하는 상수

  • watchOS complications를 위한 푸시 타입
    • swift
        static let complication: PKPushType
      
    • objectiveC
        const PKPushType PKPushTypeComplication;
      
  • file provider updates를 위한 푸시 타입
    • swift
        static let fileProvider: PKPushType
      
    • objectiveC
        const PKPushType PKPushTypeFileProvider;
      
  • VoIP를 위한 푸시 타입
    • swift
        static let voIP: PKPushType
      
    • objectiveC
        const PKPushType PKPushTypeVoIP;
      

VoIP Push

iOS8부터 애플은 새로운 종류의 푸시인 VoIP푸시를 소개했다.
새로운 VoIP Push에는 두개의 장점이 있다.

  • 푸시를 허용할 필요가 없다; 사용자 모르게 작동한다.
  • 애플은 이러한 푸시 알림을 최우선으로 제공할 것을 약속했다.

단점으로는

  • iOS8이상에서만 작동한다.

VoIP에 대해 간단히 알아보면,

사용자가 장치의 셀룰러 서비스 대신 인터넷 연결을 사용하여 전화를 걸고 받을 수 있다.

VoIP푸시 알림을 사용하여 영구 연결 방지하기

과거에는 VoIP앱이 수신 통화 및 기타 데이터를 수신하기 위해 서버와의 지속적인 네트워크 연결을 유지해야 했다. 이 말은 앱이 사용되지 않을 때 조차도 연결 상태를 유지하기 위해 앱과 서버 간에 주기적인 메시지를 주고받는 복잡한 코드를 작성해야 했다는 것이다. 이로 인해 에너지 낭비를 야기했다. 또한 VoIP앱을 중단할 경우, 서버로부터의 전화를 더 이상 받지 못하게 된다는 것을 의미한다.

개발자들은 지속적인 연결 대신에 앱이 원격 서버에서 푸시(데이터 있을 경우)를 받을 수 있도록 해주는 PushKit 프레임워크를 사용해야한다. 푸시를 수신할 때 마다 앱이 실행된다. 예를 들어, VoIP앱은 통화가 수신될 때 경보를 표시할 수 있으며, 이를 수신 또는 거부할 수 있는 옵션을 제공한다. 또한 사용자가 수락하기로 한 경우에는 통화를 시작하기 위한 사전 점검 단계를 시작할 수도 있다.

PushKit를 사용하여 VoIP푸시를 수신할 경우 다음과 같은 이점들이 있다.

  • 장치는 VoIP 푸시가 발생했을 때만 깨어나 에너지를 절약한다.
  • 앱이 동작을 수행하기 전에 사용자가 응답해야 하는 표준 푸시 알림과 달리, VoIP는 처리를 위해 앱으로 곧바로 푸시된다.
  • VoIP 푸시는 우선 순위가 높은 알림으로 간주되며 지연없이 전달된다.
  • VoIP 푸시에는 표준 푸시 알림과 함께 제공되는 것보다 많은 데이터가 포함될 수 있다.
  • VoIP 푸시를 받을 때 앱이 실행 중이 아니면 앱이 자동으로 다시 시작된다.
  • 앱이 백그라운드에서 작동하는 경우에도 앱에 푸시를 처리 할 런타임이 제공된다.

우선 VoIP Push Notifications을 수신하기 위한 준비가 필요하다.
img

또한 VoIP 앱용 인증서를 만들어야한다. 각 VoIP앱에는 고유한 앱ID에 매핑 된 자체 VOIP 서비스인증서가 필요하다. 이 인증서를 시용하면 알림 서버가 VoIP 서비스에 연결할 수 있다.
img

Configure VoIP Push Notifications

VoIP push notifications를 받기 위해서 app delegate랑 PushKit Framework랑 연결을 해줘야 된다. 그런 다음 PKPushRefistry 객체를 만들고 delegate를 self로 설정해주고, VoIP 푸시를 수신하도록 등록한다.

OBJECTIVE-C

// Link to the PushKit framework
#import <PushKit/PushKit.h>
 
// Trigger VoIP registration on launch
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [self voipRegistration];
  return YES;
}
 
// Register for VoIP notifications
- (void) voipRegistration {
  dispatch_queue_t mainQueue = dispatch_get_main_queue()
  // Create a push registry object
  PKPushRegistry * voipRegistry = [[PKPushRegistry alloc] initWithQueue: mainQueue];
  // Set the registry's delegate to self
  voipRegistry.delegate = self;
  // Set the push type to VoIP
  voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
}

SWIFT

// Link to the PushKit framework
import PushKit
 
// Trigger VoIP registration on launch
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    self.voipRegistration()
    return true
}
 
// Register for VoIP notifications
func voipRegistration {
    let mainQueue = dispatch_get_main_queue()
    // Create a push registry object
    let voipRegistry: PKPushRegistry = PKPushRegistry(mainQueue)
    // Set the registry's delegate to self
    voipRegistry.delegate = self
    // Set the push type to VoIP
    voipRegistry.desiredPushTypes = [PKPushTypeVoIP]
}

다음으로, 업데이트 된 푸시 자격 증명을 처리할 delegate 메서드를 구현한다.
앱에 표준 푸시 알림과 VoIP 푸시가 모두 수신되면 앱에 두 개의 별도 푸시 토큰이 수신된다. 알림을 받으려면 두 토큰을 모두 서버에 전달해야 된다.

OBJECTIVE-C

// Handle updated push credentials
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials: (PKPushCredentials *)credentials forType:(NSString *)type {
  // Register VoIP push token (a property of PKPushCredentials) with server
}

SWIFT

// Handle updated push credentials
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
    // Register VoIP push token (a property of PKPushCredentials) with server
}

마지막으로, 푸시를 처리할 delegate 메서드를 설정해야한다. 푸시를 받으면 앱이 실행 중이 아닌 경우 앱이 자동으로 실행된다.

OBJECTIVE-C

// Handle incoming pushes
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
  // Process the received push
}

SWIFT

// Handle incoming pushes
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
    // Process the received push
}


아래 부터는 Push Notification에 관한 것

What’s New in the Apple Push Notification Service

Token Authentication

  • APNs에 연결할 때 단순화된 제공자 인증
  • 보안
  • 만들기 쉬움
  • 만료되지 않는다.

Local and Remote Notifications Overview

Local and Remote Notifications는 유저에게 앱이 foreground에서 실행중이지 않을 때에도 새로운 데이터를 앱에서 사용할 수 있을 때 알림을 주는 방법이다.

Local과 Remote의 차이는 다음과 같다.

  • Local
    • 사용자 앱은 알림 세부 정보를 로컬로 구성하고 시스템으로 전달한다.
    • 해당 앱이 foreground 상태가 아닐 때 알림을 제공한다.
    • iOS, tvOS, watchOS를 지원한다.
  • Remote
    • Apple Push Notification 서비스를 통해 사용자 기기에 데이터를 푸시 할 수 있는 회사 서버 중 하나를 사용할 수 있다.
    • iOS, tvOS, watchOS, macOS를 지원한다.

Local과 Remote Notifications 모두 scheduling과 알림을 다루기 위한 코드를 앱에 추가해야 한다. 
Remote notifications의 경우, 사용자 장치에서 데이터를 수신하여 알림 관련 데이터를 APNs(Apple Push Notification service)에 보낼 수 있는 서버 환경을 제공해야 한다. 
APNs는 사용자 장치에 대한 원격 알림 전송을 처리하는 애플에서 제공하는 서비스.


Managing Your App’s Notification Support

Local 및 Remote Notification을 지원하도록 앱이 시작할 때 설정을 해야 한다.
특히, 다음과 같은 작업을 수행하는 경우에는 미리 앱을 설정해야 한다.

  • 알림에 대한 응답으로 경고나, 소리를 재생하거나, 아이콘에 배지를 표시하는 경우
  • 알림과 함께 custom action buttons을 표시하는 경우

일반적으로, 앱이 시작 전에 모든 설정을 수행해야 된다.
iOS나 tvOS에서는, UIApplication 에 있는 application:didFinishLaunchingWithOptions:` 메서드 전에 끝나야한다.
Remote Notifications는 추가 구성이 필요하다. 이에 대한 자세한 내용은 Configuring Remote Notification Support에 나와 있다.