2017-02-26 1 views
0

xcode 10에서 swift3을 사용하여 알림 서비스를 만들고 있습니다. 이제 푸시 알림이 백그라운드에서 제공 될 때 (앱이 종료 된 경우에도) 배지는 처음에는 증가하지만 두 번째 푸시 알림에서 1 씩 증가합니다. 또한 앱에 들어가서 배경으로 돌아 왔을 때 배지 수는 정상이지만 위와 같은 문제가 다시 발생합니다.백그라운드에서 푸시 알림 배지를 다시로드하십시오.

지연 또는 로컬 알림을 통해 문제를 확인하려고했지만 문제가 무엇인지 파악하지 못했습니다.

다음은 AppDelegate 내의 알림과 관련된 알림입니다. 푸시 알림 클릭 이벤트도 정상적으로 작동합니다.

class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate, NaverThirdPartyLoginConnectionDelegate { 
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     UNUserNotificationCenter.current().delegate = self 
     UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert,], completionHandler: {(granted, error) in 
      if (granted) 
      { 
       application.registerForRemoteNotifications() 
      } 
      else{ 
      } 
     }) 
     return true 
} 

... 
... 
extension AppDelegate: UNUserNotificationCenterDelegate { 
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
     completionHandler([.badge, .alert, .sound]) 
     UIApplication.shared.applicationIconBadgeNumber = UIApplication.shared.applicationIconBadgeNumber + 1 
     } 

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
     print("userInfo: \(response.notification.request.content.userInfo)") 
     var userInfo:[AnyHashable: Any]? 
     let pushId:Int32 = userInfo?["uid"] as! Int32 
     self.moveView(pushId)// My app load method 
    } 
} 

답변

0

백그라운드 상태로 앱을 실행하는 것은 일시 중지되는 앱에 대한 간단한 중지입니다. 일시 중지 된 앱은 메모리에 남아 있지만 코드는 실행하지 않습니다. 이러한 이유로 코드가 실행되지 않아 배지 값이 업데이트되지 않습니다. 응용 프로그램 상태 및 백그라운드 실행에 대한 링크 아래에있는 링크를 참조하십시오. 이 문제를 해결하기

  • Background Execution
  • 그래서 더 나은 방법 Application State

    • 푸시 알림 페이로드 (payload)의 내부에 배지 값을 보내 보낼 수 있습니다. 예컨대

      { 
          "aps" : { 
           "alert" : { 
            "title" : "Game Request", 
            "body" : "Bob wants to play poker", 
            "action-loc-key" : "PLAY" 
           }, 
           "badge" : 5 
          }, 
          "acme1" : "bar", 
          "acme2" : [ "bang", "whiz" ] 
      } 
      

      는 로컬 알림 배지를 보여줄 필요가없는 Creating the Notification Payload

    • 프로그래밍 배지 값을 증가하지 마십시오 원격 알림 페이로드를 만들려면이 링크를 참조하십시오. 푸시 알림이 수신되는 동안 백그라운드에서 코드를 실행하려면 제한이 거의없는 VoIP push notification을 사용합니다. 예를 들어 앱은 관련 VoIP 서비스 여야합니다.

      푸시 알림 페이로드를 변경하는 것이 좋습니다.

      감사합니다.

    +0

    의견에 감사 드리며 늦어서 죄송합니다. 신속하게 신청합시다. –

    관련 문제