Notice
Recent Posts
Recent Comments
Link
관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - swift] Push Notification 간단 테스트 방법 (시뮬레이터) 본문

iOS 응용 (swift)

[iOS - swift] Push Notification 간단 테스트 방법 (시뮬레이터)

jake-kim 2023. 2. 16. 22:45

푸시 노티피케이션 테스트

시뮬레이터에서 푸시 테스트 방법

  • terminal에서 커맨드 사용 (번거로움이 있으므로 비추천)
  • .apns파일 사용 (json으로 구성된 .apns파일을 만든 후 simulator에 드래그하면 푸시가 가지만, 딥링크 푸시 및 여러가지 푸시를 만들고 테스트 하고 싶은 경우, 직관적이지 않음)
  • RocketSim 앱 사용 - 직관적인 UI를 제공하여 쉽게 테스트 가능

RocketSim 설치

  • RocketSim 홈페이지에서 설치 (앱스토어)
    •  Simulator에서 푸시 테스트를 쉽게 하기 위해 사용

  • 열기 > Select Xcode 선택 > Application에서 설치된 Xcode 선택

  • 권한 허용
    • recording 권한 - 시뮬레이터를 실행하면 자동으로 그 옆에 RocketSim 기능을 사용할 수 있는 화면을 띄워줌

(시뮬레이터를 실행하면 자동으로 그 옆에 RocketSim 기능을 사용할 수 있는 화면)

  • 시작하면 결제창과 basic으로 free 버전을 사용할 수 있는데, free 버전 선택 
  • 실행 후 위에 로켓 아이콘 > Settings 선택

  • App Actions 탭 > Add group 클릭

  • Add group > Bundle Identifier에 테스트할 앱의 번들 ID 입력 및 Add action
    • Push Notification 선택

  • json 형식 확인 후 Create 버튼 클릭

  • 같은 방식으로 test2도 추가

  • Xcode > AppDelegate에서 권한 요청 
    • requestAuthorization(options:)
import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .sound]) { granted, error in
            print(granted, error)
        }
        
        return true
    }
...
  • foreground에서도 푸시가 보이게끔 delegate 추가
let center = UNUserNotificationCenter.current()
center.delegate = self
...

extension AppDelegate: UNUserNotificationCenterDelegate {
    // foreground에서 시스템 푸시를 수신했을 때 해당 메소드가 호출
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.sound, .badge, .banner])
    }
}
  • Signing & Capabilities > + Capability 에서 Push Notification 선택

  • 시뮬레이터 실행 > cmd 심볼 클릭 > permission이 필요하다고 나오면, permission 설정 버튼을 누르고 디렉토리가 나오면 바로 open 버튼 누르면 아래 화면 등장

  • 시뮬레이터 오른쪽에서 Test1 클릭하면 푸시 노티피케이션 도착

번외) .apns파일 만들어서 테스트 방법

  • 확장자 명을 .apns로 생성
  • 일반 푸시 json 형식과 동이랗게 설정 후 Simulator Target Bundle이라는 키값과 bundle id 값을 추가

(test3.apns)

{
  "Simulator Target Bundle": "com.jake.ExPushNotification",
  "aps": {
    "alert": {
      "title": "푸시 테스트",
      "body": "iOS 앱 개발 알아가기 jake 블로그"
    }
  }
}
  • simulator에 드래그하면 푸시 노티피케이션 동작

* 참고

https://www.avanderlee.com/workflow/testing-push-notifications-ios-simulator/

Comments