투케이2K

60. (ios/swift) 파이어베이스 푸시 알림 fcm 메시지 http 프로토콜 사용해 전송 실시 - talend api 본문

IOS

60. (ios/swift) 파이어베이스 푸시 알림 fcm 메시지 http 프로토콜 사용해 전송 실시 - talend api

투케이2K 2021. 11. 28. 10:43
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : SWIFT

 

[방법 설명]

 
// [파이어베이스 푸시 요청 주소]
post 방식 : https://fcm.googleapis.com/fcm/send



// [파이어베이스 푸시 요청 json 타입]
{
  "notification": {
    "title" : "testTitle",
    "body" : "testContent",
    "sound" : "default",
    "badge" : 0
  },
  "data" : {
    "key_1" : "Value_1",
    "key_2" : 2
  },
  "content_available": true,
  "mutable_content": true,
  "priority" : "high",
  "to": "fh1arwyHd0swrLQd4JVMorNPmGHqtuDJnAtW1lzae5fdTvmg ... "
}
 

[소스코드 : AppDelegate]

// MARK: - [노티피케이션 알림 딜리게이트 추가]
// [SEARCH FAST] : [포그라운드 백그라운드 알림 창 표시]
extension AppDelegate: UNUserNotificationCenterDelegate {
    
    // [앱이 foreground 상태 일 때, 알림이 온 경우]
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("")
        print("===============================")
        print("[AppDelegate >> willPresent]")
        print("--------------------")
        print("설명 :: 앱 포그라운드 상태 푸시 알림 확인")
        print("--------------------")
        print("userInfo :: \(notification.request.content.userInfo)") // 푸시 정보 가져옴
        print("--------------------")
        print("title :: \(notification.request.content.title)") // 푸시 정보 가져옴
        print("--------------------")
        print("body :: \(notification.request.content.body)") // 푸시 정보 가져옴
        print("--------------------")
        print("data [keys] :: \(notification.request.content.userInfo.keys)") // 푸시 정보 가져옴
        print("--------------------")
        print("data [key_1] :: \(notification.request.content.userInfo["key_1"] as! String)") // 푸시 정보 가져옴
        print("--------------------")
        print("data [key_2] :: \(notification.request.content.userInfo["key_2"] as! String)") // 푸시 정보 가져옴
        print("===============================")
        print("")
        
        // [completionHandler : 푸시 알림 상태창 표시]
        // completionHandler([.banner, .list, .badge, .sound])
        completionHandler([.alert, .badge, .sound])
    }

    // [앱이 background 상태 일 때, 알림이 온 경우]
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        print("")
        print("===============================")
        print("[AppDelegate >> didReceive]")
        print("--------------------")
        print("앱 백그라운드 상태 푸시 알림 확인")
        print("--------------------")
        print("userInfo :: \(response.notification.request.content.userInfo)") // 푸시 정보 가져옴
        print("--------------------")
        print("title :: \(response.notification.request.content.title)") // 푸시 정보 가져옴
        print("--------------------")
        print("body :: \(response.notification.request.content.body)") // 푸시 정보 가져옴
        print("===============================")
        print("")

        // [completionHandler : 푸시 알림 상태창 표시]
        completionHandler()
    }
}

 

반응형
Comments