141

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?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
user1960169
  • 3,533
  • 12
  • 39
  • 61

6 Answers6

315

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.

Background Modes dropdown list in Project Settings

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • 5
    I have already checked that field but still getting same error . – Anuj May 01 '18 at 09:39
  • Anuj see my answer below – Joshua Cleetus Dec 11 '18 at 16:37
  • 3
    I don't agree with this answer. You should only enable Background modes -> Remote notifications if you are using a remote notification to do a background update. Please refer to the docs here [Configuring a Background Update Notification](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html) – jzeferino Jul 24 '19 at 10:17
  • 1
    @jzeferino Thanks for the notice, I've added some clarification to my answer. Note that the original question was about the `application(_:didReceiveRemoteNotification:fetchCompletionHandler:)` function, which is used for handling data fetch. – Tamás Sengel Jul 24 '19 at 11:02
  • This is great, but answers the question of if you need the capability for background updates, which of course you do. I posted an answer about whether you need to set the background modes capability simply for push notifications (spoiler, you don't). – mobob Nov 30 '19 at 18:02
49

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

<dict>
<key>UIBackgroundModes</key>
    <array>
        <string>remote-notification</string>
    </array>
Aleksandr B.
  • 505
  • 4
  • 6
11

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

I know I'm splitting hairs 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.

shim
  • 9,289
  • 12
  • 69
  • 108
mobob
  • 849
  • 10
  • 17
5

Select your .xcodeproj file from Project navigator then go to Signing & Capabilies and then from library (command + shift + l or Click (+ Capability)) search for Background Modes after that, drag and drop it to Signing & Capabilities then check Remote notifications and Background processing. Also (important) add Push notifications by clicking + Capability

GO.exe
  • 646
  • 7
  • 13
4

If you don't find a list containing background modes in Signing & Capabilities, just add "Required background modes" in your project's Info.plist. Now, you will get a list containing Remote notifications under the tab Signing & Capabilities, just check it.

Md Rais
  • 972
  • 1
  • 12
  • 21
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.

Joshua Cleetus
  • 624
  • 6
  • 14