2016-11-23 1 views
2

푸시 알림을 보내고 응용 프로그램이 포 그라운드에있는 경우 willPresent이 호출됩니다. didReceive은 호출되지 않습니다. 응용 프로그램이 백그라운드에서 푸시 알림을 받으면 경고가 표시되지만 응용 프로그램에서는 didReceive 또는 willPresent을 호출하지 않습니다.UNUserNotificationCenter가 수신되지 않았습니다.

프로젝트> 기능에서 백그라운드 모드 Location updatesRemote notifications을 확인했습니다. 위치 업데이트는 관련없는 코드 용입니다.

푸시 알림을 사용하도록 설정했습니다. 그들은 잘 받고 있습니다.

내가 내 AppDelegateUNUserNotificationCenter 알림 물건을 구현 한, 아래 참조 : 왜 didReceive하지

import UserNotifications 
... 

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { 
... 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     ... 
     registerForPushNotifications(application: application) 
     ... 
    } 

    // MARK: - Push Notifications 

    func registerForPushNotifications(application: UIApplication) { 
     let notificationCenter = UNUserNotificationCenter.current() 
     notificationCenter.delegate = self 
     notificationCenter.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in 
      if (granted) 
      { 
       UIApplication.shared.registerForRemoteNotifications() 
      } 
      else{ 
       //Do stuff if unsuccessful... 
       print("Unsuccessful in registering for push notifications") 
      } 
     }) 
    } 

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
     //convert the deviceToken to a string 
     let deviceTokenString = deviceToken.map { String(format: "%02.2hhx", arguments: [$0]) }.joined() 

     let ud = UserDefaults.standard 
     ud.set(deviceTokenString, forKey: "deviceToken") 
    } 

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
     //Handle the notification 
     print("User Info = ",notification.request.content.userInfo) 
     completionHandler([.alert, .badge, .sound]) 
    } 

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
     //handle the notification 
     print("Push notification: ",response.notification.request.content.userInfo) 

     ... code to import the notification data into Core Data. 

     completionHandler() 
    } 

... 
} 

어떤 아이디어를 호출되고?

이것은 수신 된 푸시 알림 :

[AnyHashable("aps"): { 
    alert = "[name] has added you as a friend."; 
    category = "NEWS_CATEGORY"; 
    "related_user_id" = 55; 
    sound = default; 
    type = "friend_request"; 
}] 

아이폰 OS 10.1, 스위프트 3. 알림 배너를 눌러 경우 willPresent 메소드가 호출됩니다

+0

당신이 우는 소리 IOS 10.0에서 테스트 (알림을 밀어을, 아래로 스 와이프 등)를 호출 할 것인가? –

+0

@jigneshVadadoriya no, ios 10.1 – toast

답변

0

.

배너를 탭하지 않고 백그라운드에서 푸시 알림을 처리하려면 { "content-available": 1}을 알림 페이로드에 추가해야합니다. 이 유형의 푸시 알림은 silent 푸시라고합니다. 이것은 이 아니며 일반 푸시와 같이 알림 배너/알림을 생성하므로 직접 배너를 만들어야합니다.

"콘텐츠 사용 가능"으로 푸시 알림을 받으면 앱이 백그라운드에있을 때 didReceive 대리자 메서드가 호출됩니다.

N.B. plist 파일의 UIBackgroundModes 키에 '원격 통지'를 추가해야합니다.

관련 문제