背景:
應(yīng)用收到推送通知后嘹履,通過 application:didReceiveRemoteNotification:fetchCompletionHandler: 方法,響應(yīng)推送通知的點擊渐排。測試發(fā)現(xiàn)在 iOS 10 上面雖然能收到推送,但是點擊通知中心的消息進(jìn)入應(yīng)用后赖临,應(yīng)用沒有跳轉(zhuǎn)到消息頁面鲤脏。
詳情:
調(diào)試時發(fā)現(xiàn),iOS 處理推送通知的方式在 iOS 9 和 iOS 10 上面有所不同厨姚。接收消息推送有兩個代理方法:
方法 1:application:didReceiveRemoteNotification:
方法 2:application:didReceiveRemoteNotification:fetchCompletionHandler:
當(dāng)同時實現(xiàn)方法兩個方法時衅澈,應(yīng)用在不同狀態(tài)下調(diào)用的方法不一致。
一般應(yīng)用有三種狀態(tài):
a)應(yīng)用未啟動谬墙;
b)應(yīng)用已啟動矾麻,但處于后臺纱耻;
c)應(yīng)用處于前臺。
對于以上三種狀態(tài)险耀,iOS 9 在收到消息通知后都會調(diào)用方法 2弄喘,這也符合官方文檔的描述。然而對于 iOS 10甩牺,當(dāng)應(yīng)用處于 a 或 b 狀態(tài)時蘑志,調(diào)用方法 1,當(dāng)應(yīng)用處于 c 狀態(tài)時贬派,調(diào)用方法 2急但。
然而官方文檔對于這兩個方法的描述,是這樣的:
Let’s review the possible scenarios that can arise when the system delivers a local notification or a remote notification for an app.
The notification is delivered when the app isn’t running in the foreground.In this case, the system presents the notification, displaying an alert, badging an icon, perhaps playing a sound, and perhaps displaying one or more action buttons for the user to tap.
The user taps a custom action button in an iOS 8 notification.In this case, iOS calls either?application:handleActionWithIdentifier:forRemoteNotification:completionHandler:or application:handleActionWithIdentifier:forLocalNotification:completionHandler:. In both methods, you get the identifier of the action so that you can determine which button the user tapped. You also get either the remote or local notification object, so that you can retrieve any information you need to handle the action.
The user taps the default button in the alert or taps (or clicks) the app icon.If the default action button is tapped (on a device running iOS), the system launches the app and the app calls its delegate’s?application:didFinishLaunchingWithOptions:method, passing in the notification payload (for remote notifications) or the local-notification object (for local notifications). Although application:didFinishLaunchingWithOptions:isn’t the best place to handle the notification, getting the payload at this point gives you the opportunity to start the update process before your handler method is called.
For remote notifications, the system also calls the?application:didReceiveRemoteNotification:fetchCompletionHandler:method of the app delegate.
If the app icon is clicked on a computer running OS X, the app calls the delegate’s?applicationDidFinishLaunching:method in which the delegate can obtain the remote-notification payload. If the app icon is tapped on a device running iOS, the app calls the same method, but furnishes no information about the notification.
The notification is delivered when the app is running in the foreground.The app calls the?application:didReceiveRemoteNotification:fetchCompletionHandler:or?application:didReceiveLocalNotification:method of the app delegate. (If application:didReceiveRemoteNotification:fetchCompletionHandler:isn’t implemented, the system calls application:didReceiveRemoteNotification:.) In OS X, the system calls?application:didReceiveRemoteNotification:
另外搞乏,在 方法 1 SDK的描述中波桩,蘋果是這么說的:
Implement the?application:didReceiveRemoteNotification:fetchCompletionHandler:method instead of this one whenever possible. If your delegate implements both methods, the app object calls the application:didReceiveRemoteNotification:fetchCompletionHandler: method.
結(jié)論:
目前 iOS 10 測試版的推送通知處理與文檔中描述的有出入,這可能是測試版的問題请敦,后續(xù)是否能夠更改未知镐躲,因此,目前最保險的方法就是侍筛,在 application:didFinishLaunchingWithOptions:?以及上述兩個方法中都檢測是否存在推送通知萤皂,并進(jìn)行處理。
測試使用的 iOS 10 版本為 10.0 (14A5335b)匣椰,測試手機(jī)為 iPhone 6裆熙,對于這個問題的其他相關(guān)描述:
1)http://www.heapoverflow.me/question-handeling-user-notifications-on-ios-10-38940193
2)http://www.openradar.me/27194349
問題還在跟進(jìn)中,隨時變更禽笑。