iOS 推送通知和 AppDelegate 方法行为
iOS push notifications and AppDelegate methods behaviour
我对许多 Whosebug 问题和网站进行了一些研究,试图弄清楚 iOS 推送通知如何影响 AppDelegate
生命周期方法以及何时(不)触发哪种方法。研究的主要重点是 "standard" iOS 推送通知(带有 alert
字段)和静默通知(仅 content-available
设置为 1
)以及 AppDelegate 的application:didReceiveRemoteNotification
和 application:didFinishLaunchingWithOptions
方法。
我不想针对不同的场景问很多问题,而是想尝试写下我尝试过的不同测试用例的陈述,然后再问你:
Is there any statement that is wrong and if yes, which one and why?
场景一:应用程序已被使用并通过点击主页按钮进入后台。
如果发送标准推送通知,在推送通知到达的那一刻,none 方法被触发,应用程序在后台保持不活动状态。一旦推送通知被点击并因此打开应用程序,application:didReceiveRemoteNotification
方法就会被调用,而 application:didFinishLaunchingWithOptions
不会被调用。我在将应用程序置于后台后以及应用程序在后台运行一个多小时后立即测试了这种情况 - 相同的行为。我想如果出于某种原因 iOS 决定在后台终止我的应用程序,这个测试用例会变成场景 2,下面的陈述 1,对吗?
如果发送静默推送通知,在推送通知到达的那一刻,application:didReceiveRemoteNotification
方法被调用而application:didFinishLaunchingWithOptions
没有被调用。
场景 2:应用已被使用并被从 运行 应用列表中滑出而被杀死。
如果发送标准推送通知,在推送通知到达的那一刻,none 的方法被触发,应用程序仍然被杀死。一旦推送通知被点击并因此打开应用程序,application:didReceiveRemoteNotification
方法就会被调用,而 application:didFinishLaunchingWithOptions
不会被调用。
如果发送静默推送通知,none 方法会被触发,因为静默推送通知无法发送到被杀死的应用程序。在发送通知后打开应用程序后,application:didFinishLaunchingWithOptions
作为正常流程的一部分被调用,没有任何推送通知信息。 application:didReceiveRemoteNotification
没有被调用。
如果您能想到我可能忘记提及的其他一些现实生活场景,我将非常感谢了解它们以及在这些情况下会发生什么。
干杯
更新 #1
感谢 Sandeep Bhandari 提供更新和其他场景。我忘了在我原来的问题中提到我正在探索应用程序到达当前 not
处于前台的应用程序的场景。
将 Sandeep 的场景添加到列表中:
场景三:应用正在使用中,推送通知到达。
如果发送标准推送通知,application:didReceiveRemoteNotification
方法将被调用。 application:didFinishLaunchingWithOptions
不会被调用。
如果发送静默推送通知,application:didReceiveRemoteNotification
方法将被调用。 application:didFinishLaunchingWithOptions
不会被调用。
场景 4:应用程序在后台运行。
如果发送标准推送通知,application:didReceiveRemoteNotification
方法将被调用。 application:didFinishLaunchingWithOptions
不会被调用。
如果发送静默推送通知,application:didReceiveRemoteNotification
方法将被调用。 application:didFinishLaunchingWithOptions
不会被调用。
我没有看到你在那里所做的陈述有任何问题,但我相信你错过了重复我能想到的另外两个场景。
应用程序在前台并接收推送通知: didReceiveRemoteNotification
一旦 APNS 被交付到 iOS 并且您可以通过在 didRecieveRemoteNotification
方法中检查应用程序状态来处理它。
应用程序在后台运行: 我相信您知道 iOS 的后台模式。如果应用程序正在使用过期处理程序,即使您通过点击主页按钮将其置于后台,应用程序仍将处于活动状态。应用程序在后台的持续时间取决于各种因素(一些教程说应用程序仍然存在 3 分钟,我不能保证)即使在这种情况下,只要 APNS 被交付到 iOS,就会调用 didReceiveRemoteNotification
.只有这一次应用程序不会在前台,但它仍然存在!!!
From an experience and digging alot on the iOS push notification. App
being in foreground or alive in background. both situations triggers
same delegate methods. only didReceiveRemoteNotification
.
静默推送通知有不同的处理程序:(content-available 1 表示静默通知)
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
}
当应用死机时。 didReceiveRemoteNotification
从未要求定期推送通知。它必须在 didFinishLaunchingWithOptions
中按如下方式处理:
// handle notification when app is closed.
let notification = launchOptions?[.remoteNotification]
if notification != nil {
self.application(application, didReceiveRemoteNotification: notification as! [AnyHashable : Any])
}
附加信息:
测试当应用程序被杀死时接收推送通知。从双击主页按钮时出现的列表中删除?
查看日志记录和进行调试的正确方法是编辑 运行 方案并选择 等待可执行文件启动:
运行 来自 xcode 的应用程序。然后从服务器发送推送通知,然后点击通知中心的通知。
我对许多 Whosebug 问题和网站进行了一些研究,试图弄清楚 iOS 推送通知如何影响 AppDelegate
生命周期方法以及何时(不)触发哪种方法。研究的主要重点是 "standard" iOS 推送通知(带有 alert
字段)和静默通知(仅 content-available
设置为 1
)以及 AppDelegate 的application:didReceiveRemoteNotification
和 application:didFinishLaunchingWithOptions
方法。
我不想针对不同的场景问很多问题,而是想尝试写下我尝试过的不同测试用例的陈述,然后再问你:
Is there any statement that is wrong and if yes, which one and why?
场景一:应用程序已被使用并通过点击主页按钮进入后台。
如果发送标准推送通知,在推送通知到达的那一刻,none 方法被触发,应用程序在后台保持不活动状态。一旦推送通知被点击并因此打开应用程序,
application:didReceiveRemoteNotification
方法就会被调用,而application:didFinishLaunchingWithOptions
不会被调用。我在将应用程序置于后台后以及应用程序在后台运行一个多小时后立即测试了这种情况 - 相同的行为。我想如果出于某种原因 iOS 决定在后台终止我的应用程序,这个测试用例会变成场景 2,下面的陈述 1,对吗?如果发送静默推送通知,在推送通知到达的那一刻,
application:didReceiveRemoteNotification
方法被调用而application:didFinishLaunchingWithOptions
没有被调用。
场景 2:应用已被使用并被从 运行 应用列表中滑出而被杀死。
如果发送标准推送通知,在推送通知到达的那一刻,none 的方法被触发,应用程序仍然被杀死。一旦推送通知被点击并因此打开应用程序,
application:didReceiveRemoteNotification
方法就会被调用,而application:didFinishLaunchingWithOptions
不会被调用。如果发送静默推送通知,none 方法会被触发,因为静默推送通知无法发送到被杀死的应用程序。在发送通知后打开应用程序后,
application:didFinishLaunchingWithOptions
作为正常流程的一部分被调用,没有任何推送通知信息。application:didReceiveRemoteNotification
没有被调用。
如果您能想到我可能忘记提及的其他一些现实生活场景,我将非常感谢了解它们以及在这些情况下会发生什么。
干杯
更新 #1
感谢 Sandeep Bhandari 提供更新和其他场景。我忘了在我原来的问题中提到我正在探索应用程序到达当前 not
处于前台的应用程序的场景。
将 Sandeep 的场景添加到列表中:
场景三:应用正在使用中,推送通知到达。
如果发送标准推送通知,
application:didReceiveRemoteNotification
方法将被调用。application:didFinishLaunchingWithOptions
不会被调用。如果发送静默推送通知,
application:didReceiveRemoteNotification
方法将被调用。application:didFinishLaunchingWithOptions
不会被调用。
场景 4:应用程序在后台运行。
如果发送标准推送通知,
application:didReceiveRemoteNotification
方法将被调用。application:didFinishLaunchingWithOptions
不会被调用。如果发送静默推送通知,
application:didReceiveRemoteNotification
方法将被调用。application:didFinishLaunchingWithOptions
不会被调用。
我没有看到你在那里所做的陈述有任何问题,但我相信你错过了重复我能想到的另外两个场景。
应用程序在前台并接收推送通知:
didReceiveRemoteNotification
一旦 APNS 被交付到 iOS 并且您可以通过在didRecieveRemoteNotification
方法中检查应用程序状态来处理它。应用程序在后台运行: 我相信您知道 iOS 的后台模式。如果应用程序正在使用过期处理程序,即使您通过点击主页按钮将其置于后台,应用程序仍将处于活动状态。应用程序在后台的持续时间取决于各种因素(一些教程说应用程序仍然存在 3 分钟,我不能保证)即使在这种情况下,只要 APNS 被交付到 iOS,就会调用
didReceiveRemoteNotification
.只有这一次应用程序不会在前台,但它仍然存在!!!
From an experience and digging alot on the iOS push notification. App being in foreground or alive in background. both situations triggers same delegate methods. only
didReceiveRemoteNotification
.
静默推送通知有不同的处理程序:(content-available 1 表示静默通知)
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
}
当应用死机时。 didReceiveRemoteNotification
从未要求定期推送通知。它必须在 didFinishLaunchingWithOptions
中按如下方式处理:
// handle notification when app is closed.
let notification = launchOptions?[.remoteNotification]
if notification != nil {
self.application(application, didReceiveRemoteNotification: notification as! [AnyHashable : Any])
}
附加信息:
测试当应用程序被杀死时接收推送通知。从双击主页按钮时出现的列表中删除?
查看日志记录和进行调试的正确方法是编辑 运行 方案并选择 等待可执行文件启动:
运行 来自 xcode 的应用程序。然后从服务器发送推送通知,然后点击通知中心的通知。