Do remote push notifications require to add UIBackgroundModes in Info.plist?

随声附和 提交于 2020-02-26 05:11:33

问题


I have integrated remote push notifications, but I am getting this warning:

didReceiveRemoteNotification:fetchCompletionHandler:], but you still need to add "remote-notification" to the list of your supported UIBackgroundModes in your Info.plist.

My Xcode version is 8.3.1. I really want to add this to Info.plist. I have followed some tutorials as well but they didn't mentioned this either. What should I really do?


回答1:


Yes, you should enable Background Modes/Remote notifications to be able to use remote notifications for background updates.

The easiest way to do this is via the project settings. Navigate to Targets -> Your App -> Capabilities -> Background Modes and check Remote notifications. This will automatically enable the required settings.




回答2:


You can also edit needed info.plist (Open As -> Source Code) and paste :

<dict>
<key>UIBackgroundModes</key>
    <array>
        <string>remote-notification</string>
    </array>



回答3:


It happened to me even after setting the remote notifications background mode in the capabilities. The issue was I had three targets, one for production, one for qa and one for staging. I had to set remote notifications in all the three targets and that fixed the warning.




回答4:


In fact, you do not need to add UIBackgroundModes to .plist simply to use remote notifications.

I know I'm splitting heirs a bit (the other answer is mostly great, and perhaps something is new as of iOS 11), but the question refers to push notifications necessitating background updates, and they do not.

The distinction here, is that there are two different methods that accept notifications on the AppDelegate;

This one does not require you to use UIBackgroundModes:

optional func userNotificationCenter(_ center: UNUserNotificationCenter, 
                         willPresent notification: UNNotification, 
               withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

The above replaces the deprecated as of iOS 11:

optional func application(_ application: UIApplication, 
didReceiveRemoteNotification userInfo: [AnyHashable : Any])

And this one does require background modes capability:

optional func application(_ application: UIApplication, 
didReceiveRemoteNotification userInfo: [AnyHashable : Any], 
   fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

The key thing here, is the former one (and the deprecated one it replaced) only runs when the app is in the foreground. The latter will run if the app is in the foreground OR background. See the spec for this specific nugget:

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.

Hence, if you need push notifications, then decide if you need to run in the background - only if you need both should you implement the method suggested by the warning.



来源:https://stackoverflow.com/questions/45440627/do-remote-push-notifications-require-to-add-uibackgroundmodes-in-info-plist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!