본문으로 건너뛰기

Client SDK - iOS

Notifly iOS SDK는 노티플라이를 iOS 어플리케이션과 연동하기 위해 사용할 수 있습니다. 다음과 같은 기능들을 지원합니다:

  • 기기 정보를 노티플라이에 등록하여 노티플라이를 통해 발송된 앱 푸시, 인앱 팝업을 iOS 앱에서 수신할 수 있습니다.
    • 노티플라이의 인앱 팝업은 앱이 Foreground 상태일 때만 수신됩니다. Background 및 Quit 상태일 때는 무시됩니다.
  • 이벤트, 유저 정보를 노티플라이와 연동하여 모든 캠페인에서 활용할 수 있습니다.
  • 캠페인의 성과를 측정할 수 있도록 이벤트를 로깅합니다.

노티플라이에서는 푸시를 발송하기 위해 Firebase Cloud Messaging을 활용하고 있습니다. 다음 과정을 먼저 진행해주세요:

  1. Firebase 프로젝트 연동
  2. iOS APNs 인증 정보 설정

1. Capability 설정

  1. 대상 Project의 iOS Deployment Target과 Target의 Minimum Deployments iOS를 13.0 혹은 그 이상으로 설정합니다.

  2. Push Notification Capability를 추가합니다. Add Capabilities

  3. Background Modes Capability를 추가합니다. - Remote notifications과 Background Fetch를 선택합니다. Add Capabilities

2. iOS SDK 설치

Notifly iOS SDK는 CocoaPods 또는 Swift Package Manager를 통해 설치할 수 있습니다.

2-1. CocoaPods

  1. Xcode 프로젝트에 CocoaPods 설치가 되어있는지 확인합니다. 설치가 되어있지 않은 경우 CocoaPods 설치 가이드를 참고해 주세요.
  2. Podfile 파일의 상단에 platform :ios, '13.0' 또는 그 이상의 버전으로 입력해 주세요.
  3. Podfile 파일에 다음 코드를 추가해 주세요.
Podfile (root)
target 'YOUR_PROJECT_NAME' do

pod 'notifly_sdk'

end

2-2. Swift Package Manager

  1. Xcode 프로젝트에 Swift Package Manager 설치가 되어있는지 확인합니다. 설치가 되어있지 않은 경우 Swift Package Manager 설치 가이드를 참고해 주세요.
  2. Project's Settings > Package Dependencies > + 버튼을 클릭합니다.
  3. 다음 URL을 입력하고 Next를 클릭합니다.
    https://github.com/team-michael/notifly-ios-sdk
  4. Add Package를 클릭합니다.

3. Notifly 연동 코드 작성

3-1. (SwiftUI) AppDelegate.swift 파일 수정

만약, SwiftUI프로젝트를 사용하고 계시다면, AppDelegate.swift 파일이 없을 수 있습니다. 이 경우, 다음과 같이 AppDelegate.swift 파일을 생성해 주세요.

이미 존재한다면 3-2로 넘어가 주세요.

AppDelegate.swift (새로 추가할 파일)
import UIKit

class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
}
<YOUR_PROJECT_NAME>App.swift (기존 파일)
struct <YOUR_PROJECT_NAME>App: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
}

3-2. Notifly 연동 코드 작성

AppDelegate.swift 파일 에서 다음 코드들을 추가해 주세요.

  • notifly_sdk 프래임워크를 import 합니다.
import notifly_sdk
  • 다음 AppDelegate.swift의 UIApplicationDelegate 함수들 내에 각각 Notifly코드를 추가해 주세요.
  1. application(_:didFinishLaunchingWithOptions:)
    • Notifly SDK 초기화에 앞서 Firebase SDK를 초기화 합니다. (Required)
    • PROJECT_ID, USERNAME, PASSWORD 는 노티플라이 홈페이지의 설정 페이지의 SDK, API 인증정보 섹션에서 확인하실 수 있습니다.
    • 개발/프로덕션에 따라 PROJECT_ID가 다릅니다.
    • Notifly SDK를 초기화 합니다.
  2. application(_:didRegisterForRemoteNotificationsWithDeviceToken:)
    • APNs 토큰을 노티플라이에 등록합니다.
  3. application(_:didFailToRegisterForRemoteNotificationsWithError:)
    • APNs 토큰 등록에 실패한 경우, 노티플라이에 알립니다.
Example Code in AppDelegate.swift
import notifly_sdk

class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_: UIApplication,
didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
/* ...YOUR CODE */

FirebaseApp.configure() // if you don't configure Firebase SDK yet.
Notifly.initialize(projectID: "YOUR_PROJECT_NAME", username: "YOUR_USERNAME", password: "YOUR_PASSWORD")
UNUserNotificationCenter.current().delegate = self
return true
}

func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
/* ...YOUR CODE */

Notifly.application(application,
didFailToRegisterForRemoteNotificationsWithError: error)
}

func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
/* ...YOUR CODE */

Notifly.application(application,
didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}

/* ...YOUR CODE */
}

  • 앱 푸시 알림 트래픽을 노티플라이에도 전달하도록 UNUserNotificationCenterDelegate 프로토콜에 다음 코드를 추가합니다.
    1. userNotificationCenter(_:willPresent:withCompletionHandler:)
      • 앱이 실행 중일 때, 앱 푸시 알림 트래픽을 노티플라이로 전달합니다.
    2. userNotificationCenter(_:didReceive:withCompletionHandler:)
      • 앱 푸시 알림 클릭 이벤트를, 노티플라이로 전달합니다.
Example Code of UNUserNotificationCenterDelegate
import notifly_sdk

class AppDelegate: UIResponder, UIApplicationDelegate {
/* ...YOUR CODE */
}

// Extension of AppDelegate conforming to the UNUserNotificationCenterDelegate protocol
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ notificationCenter: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completion: () -> Void) {
/* ...YOUR CODE */

Notifly.userNotificationCenter(notificationCenter,
didReceive: response)

/* ...YOUR CODE */
completion()
}

func userNotificationCenter(_ notificationCenter: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completion: (UNNotificationPresentationOptions) -> Void) {
/* ...YOUR CODE */

Notifly.userNotificationCenter(notificationCenter,
willPresent: notification,
withCompletionHandler: completion)
}
}

4. 유저 프로퍼티 등록하기

  • 노티플라이에서는 유저의 아이디 (userId) 및 프로퍼티 (userProperties)를 설정하여 마케팅 캠페인 집행 시에 활용할 수 있습니다.

    • 노티플라이에서는 채널 별 푸시 알림 수신 동의 여부를 유저 프로퍼티로 설정하여, 푸시 알림 전송 전에 필터링 할 수 있습니다.
    • 카카오 알림톡, 친구톡, 문자 발송을 위해서는 전화번호를 유저 프로퍼티로 설정해야 합니다.
    • 이메일 발송을 위해서는 이메일 주소를 유저 프로퍼티로 설정해야 합니다.
    • 전화번호와, 이메일의 프로퍼티 필드명은 꼭 각각 $phone_number, $email로 설정해 주세요.
    • Notifly SDK 초기화 코드 추가를 마친 후 프로퍼티 등록을 시작해 주세요.
    • (권장사항) 로그아웃 시 userId를 nil로 하여 setUserId 호출함으로써 유저의 userId 등록을 해지해 주세요.
    • 유저 아이디 등록해지를 호출할 경우, 유저 프로퍼티 및 캠페인 피로도 관리 데이터 등 모든 유저 정보가 삭제됩니다.

4-1. userId 등록

ParameterTypeRequired
userIdStringNo
Example Swift code for Notifly.setUserId
import notifly_sdk

// set userId
Notifly.setUserId(userId: "exampleUserId")

// Unregister userId
Notifly.setUserId(userId: nil)

4-2. userProperties 등록

ParameterTypeRequired
userProperties[String:Any]Yes
Example Swift code for Notifly.setUserProperties
import notifly_sdk

let sampleUserProperties = [
"age": 15,
"sex": "male",
"push_allowed": true
] as [String : Any]

Notifly.setUserProperties(userProperties: sampleUserProperties)

5. 이벤트 로깅

  • 노티플라이에서는 유저의 행동 등 이벤트를 트래킹하여 캠페인 집행 시 타겟팅에 활용할 수 있습니다. 트래킹 된 이벤트는 푸시 알림 발송 타이밍, 발송 대상 설정 등에 활용할 수 있습니다.
    • Notifly SDK 초기화 코드 추가를 마친 후 이벤트 로깅을 시작해 주세요.
  • segmentation_event_param_keys를 활용하여 이벤트 변수 (event_params)를 발송 대상 설정 등에 활용할 수 있습니다. 이를 위해서, 발송 대상 설정에 사용할 event params의 특정 field의 key 값을 segmentation_event_param_keys에 지정해주세요.
    • 현재는 segmentation_event_param_key를 한 개까지 지원하고 있기 때문에, segmentation_event_param_keys는 길이는 1이하인 List이어야합니다.

5-1. 이벤트 로깅

ParameterTypeRequired
eventNameStringYes
eventParams[String:Any]No
segmentationEventParamKeys[String]No
Example Swift code for Notifly.trackEvent
import notifly_sdk

...
Button(action: {
Notifly.trackEvent(eventName: "click_button_1")
}) {
Text("버튼1")
.padding(5)
.background(Color.red)
.foregroundColor(.white)
.cornerRadius(10)
}

...

Button(action: {
Notifly.trackEvent(
eventName: "ticket_purchase",
eventParams: [
"ticket_name": "exampleTicket",
"ticket_price": 10000,
"ticket_purchase_date": "2021-01-01"
],
segmentationEventParamKeys: ["ticket_name"]
)
}) {
Text("티켓 구매하기")
.padding(5)
.background(Color.black)
.foregroundColor(.white)
.cornerRadius(10)
}
...

6. (권장) Notification Service Extension 설정

  • Notifly의 안내에 따라, Notification Service Extension을 설정하시면 다음 기능들이 지원됩니다.
    1. 푸시 알림에 이미지 및 비디오를 첨부할 수 있습니다.
    2. 푸시 수신 지표를 수집할 수 있어, 캠페인 성과를 보다 자세하게 확인하실 수 있습니다.

6. 연동 테스트

Client SDK - 연동 Test 섹션에서 테스트를 진행합니다.

7. (Advanced) 푸시 알림 동의 프롬프트

(Advanced) Client SDK - iOS 를 참고해 주세요.

FAQ

  • Q. 이미 Firebase Cloud Messaging을 사용 중인데 어떻게 해야 할까요?

    • A. Notifly iOS SDK는 기존 앱에서 사용하고 있을 수 있는 Firebase Cloud Messaging과 함께 사용하실 수 있습니다.
  • Q. SwiftUI를 사용하고 있습니다. 연동 후, 앱 푸시는 작동하는데 인앱 팝업이 작동하지 않는데 무엇이 문제일까요?

    • A. Info.plist에 FirebaseAppDelegateProxyEnabledNO(BOOL)로 설정해주세요.
    • A. Firebase 공식 문서에 따르면 특정 문제로 인해 SwiftUI를 사용할 때 FirebaseAppDelegateProxyEnabled를 NO로 설정해야만 정상적으로 작동한다고 합니다.