Views 1782 Votes 0 Comment 0
Atachment
Attachment '4'
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

simulator_apns01.jpg



Xcode 11.4 베타부터 가능!


iOS 애플리케이션의 푸시알림(APNS) 기능을 디버깅 하기는 어려웠다. APNS는 실제 디바이스에서만 동작하고 시뮬레이터에서는 동작하지 않기 때문이다. 그리고 APNS를 보내기 위해서는 앱의 인증서와 프로비저닝이 설정되어 있어야 한다.

하지만, Xcode 11.4 베타 부터는 iOS 시뮬레이터에서도 푸시 알림을 테스트 할 수 있게 되었다.

Xcode 11.4 베타 릴리즈노트: https://developer.apple.com/documentation/xcode-release-notes/xcode-11_4-release-notes




AppDelegate.swift파일에서 import UIKit 다음 줄에 Apple의 UserNotifications프레임 워크를 임포트 한다.


import UserNotifications

. . .

func registerForPushNotifications() {
    UNUserNotificationCenter.current()
        .requestAuthorization(options: [.alert, .sound, .badge]) {(granted, error) in
            print("Permission granted: \(granted)")
    }
}



AppDelegate의 application(_:didFinishLaunchingWithOptions:)메서드 에서 방금 만든 위의 registerForPushNotifications() 함수를 추가한다.


# AppDelegate.swift 파일

//
//  AppDelegate.swift
//
import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        registerForPushNotifications()
        return true
    }

    // MARK: UISceneSession Lifecycle
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
    
    func registerForPushNotifications() {
        UNUserNotificationCenter.current()
            .requestAuthorization(options: [.alert, .sound, .badge]) {
                (granted, error) in
                print("Permission granted: \(granted)")
        }
    }
    
}


이 후, 앱을 실행하면 다음과 같이 앱이 푸시 알림을 받을 수 있도록 권한을 요청하는 대화상자가 표시된다. 허용을 선택한다.


simulator_apns02.jpg




다음과 같은 콘솔 명령을 통해 시뮬레이터에서 푸시 알림을 받아볼 수 있다. 명령에 사용되는 파라미터는 아래에 계속 설명되어 있다.


# 시뮬레이터에 푸시 알림 보내는 명령

xcrun simctl push <device-identifier> com.example.app ExamplePushJsonData.apns


위의 com.example.app과 같은 번들 식별자는 JSON 파일에서 "Simulator Target Bundle" 키와 해당 값을 제공할 경우 명령에서는 생략이 가능하다.

<device-identifier>는 시뮬레이터의 장치 식별자로 다음과 같은 방법으로 알아낼 수 있다.


simulator_apns03.jpg




# 기본 ExamplePushJasonData.apns 파일

{
    "aps": {
        "alert": "Push Notifications Test",
        "sound": "default",
        "badge": 1
    }
}


# "Simulator Target Bundle" 키가 포함된 ExamplePushJasonData.apns 파일

{
    "Simulator Target Bundle": "np.com.sagunrajlage.TestPushNotifications",
    "aps": {
        "alert": "Push Notifications Test",
        "sound": "default",
        "badge": 1
    }
}




명령 실행 시 다음과 같은 오류가 나올 경우 Xcode Command-line Tools 업데이트가 필요하고 다음과 같은 명령으로 설치할 수 있다.


xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun



# Xcode Command-line Tools 설치/업데이트 명령

xcode-select --install



설치 후, Xcode -> Preferences -> Locations 탭에서 다음과 같이 Command Line Tools 버전을 선택한다. 이후 Xcode와 시뮬레이터를 다시 시작하고 푸시 알림을 보내는 명령을 실행하면 잘 동작할 것이다.


simulator_apns04.jpg




[참고] https://stackoverflow.com/questions/52522565/git-is-not-working-after-macos-update-xcrun-error-invalid-active-developer-pa




?

List of Articles
No. Category Subject Author Date Views
46 Etc 양성/음성 오류에 대한 개념 hooni 2013.04.23 19851
45 Etc [web] 제로보드 XE 템플릿에서 if문에 대해서 알아봅시다 hooni 2013.08.16 19805
44 Etc [세미나] XML 레포트.. hooni 2003.04.23 19702
43 Etc 정보시스템(정보보안)의 위험관리 설명 hooni 2013.04.23 19335
42 Etc GSM에서 음성이 실리는 과정 요약.. hooni 2013.04.23 17518
41 Etc 개발자가 알아야할 10가지 보안팁으로 코드 보호하기 hooni 2013.04.23 16403
40 Etc [php] 싸이월드 이미지 외부 링크 하기(php) hooni 2013.04.23 16351
39 Etc [flash] 페이지 이동 (액션스크립트) file hooni 2013.04.23 16263
38 Etc iOS 에서 쓸만한 오프라인 구글지도 찾기 hooni 2014.01.06 16035
37 Etc [doc] 웜 프레임워크 검증환경 구축(작성중..) file hooni 2013.04.23 16013
36 Etc 여기저기서 모은 VoIP(인터넷전화) 자료들~ file hooni 2013.04.23 15966
35 Etc 티스토리 테이블 html,css 구문 hooni 2013.11.03 15952
34 Etc 영어의 12 시제 (The twelve tenses of English) hooni 2013.07.12 15699
33 Etc 여러 대학 및 권위있는 기관 강좌 모음 ㅋㅋ hooni 2013.06.17 15119
32 Etc [htm] DOM에 대해 ㅎㅎ file hooni 2003.04.23 14717
31 Etc 선과 악에 대한 영어논술문항(지킬앤하이드 독서 후 이어지는 심화 수행평가) hooni 2013.12.04 12746
Board Pagination Prev 1 2 3 4 Next
/ 4