- 問題:在啟動程序時,日志里面打印了:You've implemented -[<UIApplicationDelegate> application:didReceiveRemoteNotification:fetchCompletionHandler:], but you still need to add "remote-notification" to the list of your supported UIBackgroundModes in your Info.plist.
這句話的意思是我們在UIApplicationDelegate中實現了application:didReceiveRemoteNotification:fetchCompletionHandler:方法,但沒有在Info.plist中的UIBackgroundModes添加remote-notification。之前做這個推送的同事說這個提示沒有關系,一直都有這句提示的,而且我們的推送功能一直是正常的。但是我心里還是有些疑問,那就問問度娘吧。
解決方案:
原文:http://www.cnblogs.com/yonggezai/p/4826713.html
這句話主要是提示開發者如果要支持UIBackgroundModes,需要開啟Remote notifications,具體操作可以看:iOS 7 Background Remote Notification。
看了半天,還是沒有理解Remote notifications有什么特點,后來在知乎上看到了一個問題“為什么iOS偽后台,但是有很多軟件也會在后台一直運行?”,有一個答案對remote notificaions解釋的很清楚:
推送喚醒(remote notifications)iOS7以前,當你收到推送消息時,你需要先打開應用,等待應用從網絡上獲取推送的信息之后,才能將信息呈現出來。而iOS7改變了這一過程。當系統收到推送消息時,不是首先提醒用戶,而是喚醒對應的應用,讓應用在后台獲取對應的信息。當信息處理完成后,再提醒用戶。一個很小的改變,但是可以很大的提升用戶體驗。同樣,iOS系統也會限制這種推送消息的頻率,防止系統被頻繁喚醒影響續航。
——來自知乎網友Jeffrey Lin
這個時候再結合那兩張 Apple 官方對IOS6和iOS7對比的圖片,就很容易理解了。


UIApplicationDelegate中提供了兩個方法來處理推送的回調,其中第二個方法是iOS7以后才有的:
// 如果app在前台運行,系統收到推送時會調用該方法 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { } // 不管app是在前台運行還是在后台運行,系統收到推送時都會調用該方法 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { /* Use this method to process incoming remote notifications for your app. * Unlike the application:didReceiveRemoteNotification: method, * which is called only when your app is running in the foreground, * the system calls this method when your app is running in the foreground or background. }
這兩個方法長得很像,但是職責不同,。
現在的問題是:
1.我們實現了application:didReceiveRemoteNotification:fetchCompletionHandler:方法,但沒有設置UIBackgroundModes的remote-notification,有什么影響嗎?
2.既然有了application:didReceiveRemoteNotification:fetchCompletionHandler:方法,為什么還要application:didReceiveRemoteNotification: 呢?
我測試了一下兩個遺留問題:
1.我們實現了application:didReceiveRemoteNotification:fetchCompletionHandler:方法,但沒有設置UIBackgroundModes的remote-notification,有什么影響嗎?
不設置UIBackgroundModes的remote-notification的話,除了推送喚醒功能不生效之外,沒發現有其他影響。
2.既然有了application:didReceiveRemoteNotification:fetchCompletionHandler:方法,為什么還要application:didReceiveRemoteNotification: 呢?
我看了API文檔,是這么說的:
Implement the "application:didReceiveRemoteNotification:fetchCompletionHandler:" method instead of this one(application:didReceiveRemoteNotification:) whenever possible.
If your delegate implements both methods, the app object calls the "application:didReceiveRemoteNotification:fetchCompletionHandler:" method.
所以application:didReceiveRemoteNotification:方法應該是沒用了。
轉自 祥龍Shannon
原文:http://www.jianshu.com/p/9a25418ab84e