Difference between UNUserNotificationCenterDelegate and didReceiveRemoteNotification
Asked Answered
L

1

12

How iOS behaves when we implement both didReceiveRemoteNotification as well as UNUserNotificationCenterDelegate method -

  optional public func userNotificationCenter(_ center: 
  UNUserNotificationCenter, didReceive response: UNNotificationResponse, 
  withCompletionHandler completionHandler: @escaping () -> Swift.Void)

As per documentation didReceiveRemoteNotification is deprecated but if app has implemented both delegate method, which one get call for iOS 10 app as well as iOS 9 app on XCode 8 (swift 3)?

Leonorleonora answered 24/3, 2017 at 1:35 Comment(4)
where in documentation does it say didReceiveRemoteNotification is deprecated?Voice
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo NS_DEPRECATED_IOS(3_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] or -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:] for user visible notifications and -[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] for silent remote notifications");Leonorleonora
Oh OK I see. To be honest I don't think your question has much of a value. Just don't do it. The result is unexpected. A more general question is this question, I guess that's what you meant to askVoice
Possible duplicate of What happens when you run deprecated code in swift?Voice
R
26

Basically you are asking two questions,

  1. Title: Difference between UNUserNotificationCenterDelegate and didReceiveRemoteNotification?
  2. Body: How iOS behaves when we implement both didReceiveRemoteNotification as well as UNUserNotificationCenterDelegate method

Answering #1 (according to Apple docs):

  • Deprecated: application(:didReceiveRemoteNotification:)

  • For remote-silent notifications: application(:didReceiveRemoteNotification:fetchCompletionHandler:)

  • For remote-not-silent notifications:

    • When app is on background: userNotificationCenter(:didReceive:withCompletionHandler:)

    • When app is on foreground: userNotificationCenter(:willPresent:withCompletionHandler:)

  • When app will be launched from notification (look for the notification on launchOptions?[.remoteNotification]): application(:didFinishLaunchingWithOptions:)

Answering #2:

If you implement UNUserNotificationCenterDelegate methods, application(:didReceiveRemoteNotification:fetchCompletionHandler:) will not be called for remote-not-silent notifications.

application(:didReceiveRemoteNotification:fetchCompletionHandler:) will be called only for remote-silent notifications.

If you are wondering why, you can take a look a this article

Regarding answered 11/10, 2019 at 1:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.