ios 远程推送通知需要在 Info.plist 中添加 UIBackgroundModes 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45440627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:22:05  来源:igfitidea点击:

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

iosinfo-plistuibackgroundmode

提问by user1960169

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.

didReceiveRemoteNotification:fetchCompletionHandler:],但您仍然需要将“ remote-notification”添加到 UIBackgroundMode您的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?

我的 Xcode 版本是 8.3.1。我真的很想将此添加到Info.plist. 我也遵循了一些教程,但他们也没有提到这一点。我真的应该怎么做?

回答by Tamás Sengel

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 Modesand check Remote notifications. This will automatically enable the required settings.

最简单的方法是通过项目设置。导航到Targets -> Your App -> Capabilities -> Background Modes并检查Remote notification。这将自动启用所需的设置。

Background Modes dropdown list in Project Settings

项目设置中的背景模式下拉列表

回答by Aleksandr B.

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

您还可以编辑所需的 info.plist(打开为 -> 源代码)并粘贴:

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

回答by mobob

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

事实上,你不需要简单地将 UIBackgroundModes 添加到 .plist 来使用远程通知。

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.

我知道我有点分裂继承人(另一个答案大多很好,也许是 iOS 11 的新内容),但问题是指需要后台更新的推送通知,而他们没有。

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

这里的区别在于,有两种不同的方法可以接受 AppDelegate 上的通知;

This one does not require you to use UIBackgroundModes:

这个不需要你使用 UIBackgroundModes:

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

The above replaces the deprecated as of iOS 11:

以上替换了 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 specfor 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.

使用此方法为您的应用程序处理传入的远程通知。与 application(_:didReceiveRemoteNotification:) 方法不同,该方法仅在您的应用程序在前台运行时调用,而当您的应用程序在前台或后台运行时,系统会调用此方法。

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.

因此,如果您需要推送通知,则决定是否需要在后台运行 - 只有当您同时需要两者时,才应实施警告建议的方法。

回答by Joshua Cleetus

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.

即使在功能中设置了远程通知后台模式后,它也发生在我身上。问题是我有三个目标,一个用于生产,一个用于 qa,一个用于暂存。我必须在所有三个目标中设置远程通知并修复了警告。