【问题标题】:Difference between UNUserNotificationCenterDelegate and didReceiveRemoteNotificationUNUserNotificationCenterDelegate 和 didReceiveRemoteNotification 的区别
【发布时间】:2017-03-24 01:35:16
【问题描述】:

当我们同时实现 didReceiveRemoteNotification 和 UNUserNotificationCenterDelegate 方法时,iOS 的行为方式 -

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

根据文档 didReceiveRemoteNotification 已被弃用,但如果应用程序同时实现了委托方法,那么 XCode 8 (swift 3) 上的 iOS 10 应用程序和 iOS 9 应用程序会调用哪一个?

【问题讨论】:

  • 文档中哪里说didReceiveRemoteNotification 已弃用?
  • - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo NS_DEPRECATED_IOS(3_0, 10_0, "使用 UserNotifications Framework 的 -[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] 或 -[UNUserNotificationCenterDelegate didReceiveNotificationResponse: withCompletionHandler:] 用于用户可见通知和 -[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] 用于静默远程通知");
  • 哦,好的,我明白了。老实说,我认为您的问题没有多大价值。只是不要这样做。结果出乎意料。一个更普遍的问题是this question,我想这就是你要问的意思

标签: ios swift ios10


【解决方案1】:

基本上你问了两个问题,

  1. 标题:UNUserNotificationCenterDelegate 和 didReceiveRemoteNotification 的区别?
  2. 正文:当我们同时实现 didReceiveRemoteNotification 和 UNUserNotificationCenterDelegate 方法时,iOS 的行为方式

回答 #1(根据 Apple 文档):

  • 已弃用: application(:didReceiveRemoteNotification:)

  • 对于 remote-silent 通知: application(:didReceiveRemoteNotification:fetchCompletionHandler:)

  • 对于remote-not-silent通知:

    • 当应用在后台时:userNotificationCenter(:didReceive:withCompletionHandler:)

    • 当应用在前台时:userNotificationCenter(:willPresent:withCompletionHandler:)

  • 当应用将被启动从通知中(在launchOptions?[.remoteNotification]上查找通知):application(:didFinishLaunchingWithOptions:)

回答 #2:

如果你实现了UNUserNotificationCenterDelegate 方法,application(:didReceiveRemoteNotification:fetchCompletionHandler:) 将不会被远程非静默通知调用。

application(:didReceiveRemoteNotification:fetchCompletionHandler:) 只会在远程静默通知中被调用。

如果你想知道为什么,你可以看看这个article

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 2011-10-20
    • 2020-01-23
    • 1970-01-01
    • 2017-08-16
    相关资源
    最近更新 更多