Notice
Recent Posts
Recent Comments
Link
관리 메뉴

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

[iOS - swift] 6. Push Notification 응용 - 푸시 앱 아이콘 부분 커스텀 방법 (메시지 앱에서의 썸네일 아이콘 푸시 구현, 카톡 푸시 썸네일, INSendMessageIntent) 본문

iOS 응용 (swift)

[iOS - swift] 6. Push Notification 응용 - 푸시 앱 아이콘 부분 커스텀 방법 (메시지 앱에서의 썸네일 아이콘 푸시 구현, 카톡 푸시 썸네일, INSendMessageIntent)

jake-kim 2023. 2. 22. 23:48

1. Push Notification 응용 - 테스트 방법 (Pusher, APNs)

2. Push Notification 응용 - Silent Push Notification (사일런트 푸시, 푸시를 이용한 백그라운드에서 업데이트 방법)

3. Push Notification 응용 - Rich Push Notification (Notification Service Extension, 푸시 내용 변경하여 띄우기)

4. Push Notification 응용 - 시스템 푸시에 이미지 넣기 (Notification Service Extension, mutable-content)

5. Push Notification 응용 - 푸시 커스텀 UI 구현 방법 (Notification Content Extension, category)

6. Push Notification 응용 - 푸시 앱 아이콘 부분 커스텀 방법 (메시지 앱에서의 썸네일 아이콘 푸시 구현, 카톡 푸시 썸네일, INSendMessageIntent)

직접 구현한 썸네일이 있는 푸시

시스템 푸시의 App Icon 부분 변경 방법

(디폴트 푸시)

(App Icon 변경)

  • 썸네일 아이콘 표시
  • 우측 하단에는 앱 아이콘이 표시

썸네일 아이콘 적용 방법

  • Target > signing & Capabilities > Capability > Communication Notifications 추가

  • 메인 타겟 앱 (ExPush)의 info.plist에 액티비티 타입 추가 (값은 INSendMessageIntent)

(코드)

    <key>NSUserActivityTypes</key>
    <array>
        <string>INSendMessageIntent</string>
    </array>
  • Notification Service Extension 추가
  • Notification Service Extension에서 로컬에 있는 image를 앱 아이콘 대신 띄울 것이므로 Asset폴더 생성
    • Notification Service Extension의 같은 경로에 Asset 폴더 추가

  • NotificationService의 didReceive에서 구현
import UserNotifications
import Intents // <- 추가
import UIKit // <- 추가

class NotificationService: UNNotificationServiceExtension {
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
    
    }
}
  • intent를 사용하여 UNImage를 만들어야 하는데 이 때 UIImage처럼 named로 그냥 접근하면 사진이 안나오므로 pngData로 변경하는 코드 필요
    • 뒤에 나올 IN으로 시작하는 것들은 (INImage, INPerson...) 모두 Intent와 연관된 개념
    • intent: 앱을 system level에서 제어할 수 있는 방법 (보통 Siri, shortcut, spotlight 등에 사용)
let avatar = INImage(imageData: UIImage(named: "my_image.png")!.pngData()!)

(만약 image가 없을 경우 아래처럼 default 이미지 노출)

  • person에 대한 인스턴스 생성
let senderPerson = INPerson(
    personHandle: INPersonHandle(value: "unique-sender-id-2", type: .unknown),
    nameComponents: nil,
    displayName: "Sender name",
    image: avatar,
    contactIdentifier: nil,
    customIdentifier: nil,
    isMe: false,
    suggestionType: .none
)

let mePerson = INPerson(
    personHandle: INPersonHandle(value: "unique-me-id-2", type: .unknown),
    nameComponents: nil,
    displayName: nil,
    image: nil,
    contactIdentifier: nil,
    customIdentifier: nil,
    isMe: true,
    suggestionType: .none
)
  • intent와 interaction 인스턴스 생성
// Intent
let intent = INSendMessageIntent(recipients: [mePerson],
                                 outgoingMessageType: .outgoingMessageText,
                                 content: "Message content",
                                 speakableGroupName: nil,
                                 conversationIdentifier: "unique-conversation-id-1",
                                 serviceName: nil,
                                 sender: senderPerson,
                                 attachments: nil)

intent.setImage(avatar, forParameterNamed: \.sender)
// intent.setImage(avatar, forParameterNamed: \.speakableGroupName) // 그룹

// interaction
let interaction = INInteraction(intent: intent, response: nil)
interaction.direction = .incoming
  • donate 후 intent를 담은 content로 업데이트하여 푸시 쏴주기
// 알림을 주기 전에 `donate`
interaction.donate { error in
    if let error = error {
        print(error)
        return
    }
    
    do {
        // 이전 notification에 intent를 더해주고, 노티 띄우기
        let updatedContent = try request.content.updating(from: intent)
        contentHandler(updatedContent)
    } catch {
        print(error)
    }
}

* 전체 코드: https://github.com/JK0369/ExPushTest

* 참고

https://stackoverflow.com/questions/61324938/supporting-share-sheet-suggestions

https://developer.apple.com/videos/play/wwdc2021/10091/

https://developer.apple.com/documentation/usernotifications/implementing_communication_notifications

Comments