본문 바로가기

어플/Xcode

(ios) Firebase Cloud Messaging Xcode 백그라운드 알람기능

포그라운드 가져다 쓰는거가지고 하루를 잡아먹었는데..

백그라운드 하는 것도 하루를 잡아먹을뻔했습니다...

 

포그라운드에서는 xcode 업데이트 해야하는지 몰라서 하루를 뻘짓했고,,, ㅠ

백그라운드는 content_available = true로 바꿔야한다해서 이렇게도 해봤는데 결국 안되고 ㅠ

 

// [START ios_10_message_handling]
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

  // Receive displayed notifications for iOS 10 devices.
  func userNotificationCenter(_ center: UNUserNotificationCenter,
                              willPresent notification: UNNotification,
    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo

    // With swizzling disabled you must let Messaging know about the message, for Analytics
    // Messaging.messaging().appDidReceiveMessage(userInfo)
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
      print("Message ID: \(messageID)")
    }

    // Print full message.
    print(userInfo)

    // Change this to your preferred presentation option
    // completionHandler([]) 여기!!! 
    completionHandler([UNNotificationPresentationOptions.alert]) //이렇게 변경
  }

  func userNotificationCenter(_ center: UNUserNotificationCenter,
                              didReceive response: UNNotificationResponse,
                              withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
      print("Message ID: \(messageID)")
    }

    // Print full message.
    print(userInfo)

    completionHandler()
  }
}
// [END ios_10_message_handling]

 

https://github.com/firebase/quickstart-ios/blob/08d8e59e8ec738c237c86598944fb5cd0412b988/messaging/MessagingExampleSwift/AppDelegate.swift#L116-L154

 

firebase/quickstart-ios

Firebase Quickstart Samples for iOS. Contribute to firebase/quickstart-ios development by creating an account on GitHub.

github.com

Firebase에 있는 코드 그대로 가져왔습니다. 

저는 이것저것 삽질을 했는데 completionHandler([])를 completionHandler([UNNotificationPresentationOptions.alert])로 바꾸면 되더라구요.

 

저 한줄 만 바꿨을 뿐인데 백그라운드에서 알람이 옵니다.. 

진짜 힘드네요 ㅋㅋㅋ

 

포그라운드에서는 알람이 뜨는데 백그라운드에서 안뜨시는 분들은 이렇게 해보세요.

정말 해결 됐음 좋겠습니다.

쉽게쉽게 개발하자...

'어플 > Xcode' 카테고리의 다른 글

Firebase Cloud Messaging Xcode simulator Notification에러  (0) 2020.04.09