我应该使用哪种方法处理iOS远程通知?
我知道类似的问题已经被问了很多次。但它仍然是阅读那些线程后非常混乱给我,UNUserNotificationCenter
在iOS版推出后,尤其是10我应该使用哪种方法处理iOS远程通知?
的官方文档中提到3种方法在那里我可以处理远程通知:
- 实施
userNotificationCenter:willPresentNotification:withCompletionHandler:
来处理当应用程序处于前景时的通知。 - 执行
userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
当应用程序在后台或未运行。 - 但是文档中还提到:在iOS和tvOS中,系统将通知有效载荷传递给应用程序委托的
application:didReceiveRemoteNotification:fetchCompletionHandler:
方法。
所以,
- 要处理远程通知时应用程序在后台/无效,应我把我的代码的应用程序委托方法在3或2的通知中心委托?由于UNUserNotificationCenter仅适用于iOS> 10,我应该编写不同的代码来处理每个案例吗?
- 关于1,它仅在iOS 10之后提供。如何在iOS 10之前的应用程序在前台运行时处理远程通知?
而且,更令人困惑的是:如果应用程序在后台,则何时调用委托方法:何时收到通知消息?或者当用户点击通知?
iOS的10及更高版本:
1)userNotificationCenter willPresent通知:一般用来决定何时用户已经在应用程序中,并通知到达该怎么办。您可能会在应用程序内触发远程通知。在他点击远程通知后,方法2(didReceive响应)被调用。
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
//Handle push from foreground
//When a notification arrives and your user is using the app, you can maybe notify user by showing a remote notification by doing this
completionHandler([.alert, .badge, .sound])
//To print notification payload:
print(notification.request.content.userInfo)
}
2)userNotificationCenter didReceive响应:一般用于将用户重定向到用户敲击该通知后该应用的特定屏幕。
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {
//Handle push from background or closed (or even in foreground)
//This method is called when user taps on a notification
//To print notification payload:
print(response.notification.request.content.userInfo)
}
下面的iOS 10:
3)应用didReceiveRemoteNotification:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
//To print notification payload
print(userInfo)
if #available(iOS 10.0, *) {
}
else {
//Handle remote notifications for devices below iOS 10
if application.applicationState == .active {
//app is currently in foreground
}
else if application.applicationState == .background {
//app is in background
}
else if application.applicationState == .inactive {
//app is transitioning from background to foreground (user taps notification)
}
}
}
4)应用didFinishLaunchingWithOptions launchOptions:其被放置装置之下的iOS 10的唯一情形当应用程序关闭时,用户点击启动应用程序的通知。您必须检查以下方法。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//To print notification payload:
if let notification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
print(notification)
}
}
LaunchOptions是指示该应用是 推出的原因(如果有的话)的字典。在用户直接启动应用程序的情况下,此字典的内容可能为空,如 。
现在回答你的问题,
1)处理远程通知时应用程序在后台/无效,你必须添加代码方法2(userNotificationCenter didReceive响应)与设备iOS 10及以上版本。另外,对于iOS 10以下的设备,您必须使用方法3(应用程序didReceiveRemoteNotification)。
2)要在iOS 10之前的应用程序在前台运行时处理远程通知,请使用方法3活动状态。