[iOS] 사용자 귀찮게 하는법 (Local-Push)

유인호·2024년 1월 23일
0

iOS

목록 보기
21/64

서론

사용자를 귀찮게 하는 이상한 취미를 가진 개발자들이 많은데, 이떄 딱 좋은 기능이 푸시 노티피케이션이겠다. 흔히 사람들이 푸시알림이라고 부르는것들을 이 글에서 구현해 볼 수 있도록 하겠다.


1. 초기 설정

푸시 노티는 다른 하드웨어 설정(카메라, GPS등)과는 다르게 info.pilist에서 설정이 필요 없다. 하지만 코드를 적을게 좀 있는데,

// AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
		UNUserNotificationCenter.current().delegate = self
		UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { result, err in
		print(result)
	}
	return true
}

AppDelegate파일에서 코드를 작성하고, 사용할 기능들을 명시한다.

  • .alert: 흔히 푸시 알림이라고 하면 생각나는 그거 맞음.
  • .sound: 알림이 오면서 사운드가 나는데, 사용자를 귀찮게 하기 좋다.
  • .badge: 작성자 본인이 제일 싫어하는 기능인데, 카카오톡에서 카톡이 오면 앱 아이콘위에 숫자가 나오면서 몇개를 안 읽었는지 보여주는 기능인데, 작성자 같은 알림을 싫어하는 사람에게 딱 질색인 기능이다.

2. 푸시 알림 정의

아래 사진에 있는 푸시를 한번 만들어 보려고 한다.

// 노티 내용
let content = UNMutableNotificationContent()
	content.title = "푸시 알림 제목인듯함"
	content.body = "본문인듯함"
	content.badge = 20

// TimeIntervalNotification
let timeTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
    
let timeRequest = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: timeTrigger)

UNUserNotificationCenter.current().add(timeRequest)

// CalendarNotification
var componet = DateComponents()
	componet.hour = 11
	componet.minute = 24
    
let calenderTrigger = UNCalendarNotificationTrigger(dateMatching: componet, repeats: true)
        
let calenderRequest = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: calenderTrigger)

UNUserNotificationCenter.current().add(calenderRequest)

일단 대표적인 알람 두개를 등록해보았다. 하나는 등록 후 3초뒤에 푸시 알람이 나오는 코드이고, 하나는 매일 11시 24분마다 푸시 알람이 오도록 만들었다.

여기서 TimeIntervalNotification방식의 푸시 알림의 경우, repeats를 true로 하고 싶을 경우 60초 이상으로 해야한다.


결론

Local-Push를 구현해보았는데, 혹여나 사용자가 앱에 관심을 가지지 않아 걱정인 앱 개발자라면, 한번 로컬 푸시를 만들어서 사용자를 귀찮게 해보는것도 방법이 될 수 있으니 한번 시도해보길 바란다.

profile
🍎Apple Developer Academy @ POSTECH 2nd, 🌱SeSAC iOS 4th

0개의 댓글