问题描述
我在运行 iOS 8.1 的 iPad 2 中遇到了有关 APNS 的问题.我在谷歌上搜索了几天并尝试了我能找到的所有解决方案.但是我似乎无法解决这个问题.
I have this problem regarding APNS in my iPad 2 running iOS 8.1 . I searched on Google for a few days and tried every solution that I can find. However I cant seem to solve this problem.
我做了什么:
- 启用的功能:远程通知、后台获取
- 我在 aps 负载中有content-available = 1".
- 我在 didReceiveRemoteNotification:fetchCompletionHandler 中使用了 NSLog() 以便我可以在设备日志中清楚地看到该方法是否被触发
我想做什么:
我想在后台收到 APNS 时执行网络请求.我相信我可以使用 beginBackgroundTaskWithExpirationHandler 和其他东西很好地处理那部分.但是,经过调查,我发现问题是由于 didReceiveRemoteNotification:fetchCompletionHandler 在应用程序处于后台状态时未运行.
I want to perform network request when I receive the APNS in background. I believe I can handle that part well using beginBackgroundTaskWithExpirationHandler and stuff. However, going down the investigation I found that the problem is due to didReceiveRemoteNotification:fetchCompletionHandler not been run when app is in background state.
问题:
didReceiveRemoteNotification:fetchCompletionHandler 可以在应用程序处于前台时触发.但它仅在 xcode 仍在调试应用程序时在后台触发.这意味着如果我拔掉电缆(调试会话结束),它就不能再触发了.如果我重新插上它也是一样的.没有反应.
didReceiveRemoteNotification:fetchCompletionHandler can trigger when app is in foreground. But it is ONLY triggered in background when xcode is still debugging the app. Which means if I unplug the cable (the debugging session is over), it cannot trigger anymore. Same thing if I replug it back in. No response.
但是,与此同时,我仍然可以收到带有警报正文和声音的 APNS(尽管该方法仍未触发),正如我在设备日志中看到的那样.
However, at the same time, I can still receive APNS with alert body and sound (although the method is still not triggered) as I see in the Device Log.
我尝试了无声"推送通知的所有组合:声音 = ""、警报 = "" 等.但似乎没有任何效果.这里的专业人士有什么帮助吗?
I tried every combination of "silent" push notification: sound = "", alert = "", etc. But nothing seems to work. Any help from the pros here?
谢谢.
推荐答案
仔细检查后台应用刷新是否打开.我在旅行时将其关闭后遇到了一段时间.
Double check that Background App Refresh is turned on. I ran into this for a while after turning it off while traveling.
此外,您不需要在 didReceiveRemoteNotification:fetchCompletionHandler
中使用后台任务,因为它已经在后台运行.只需确保在完成网络请求后调用适当的完成处理程序即可.
Also, you shouldn't need to use a background task inside of the didReceiveRemoteNotification:fetchCompletionHandler
as it is already running in the background. Just make sure to call the appropriate completion handler when you're done with your network request.
最后,didReceiveRemoteNotification:fetchCompletionHandler
不会在常规(非静默)通知上被调用,因为常规通知不会唤醒您的应用并执行代码.
Finally, didReceiveRemoteNotification:fetchCompletionHandler
will not be called on a regular (non-silent) notification because regular notifications do not wake your app and execute code.
另一个需要检查的奇怪而晦涩的事情是通知框架数据中的优先级 - 指向 Apple 文档的链接 - 用于优先级"的命令 F 以获取详细信息.当我这样做时,我的构建通知的 Ruby gem 默认使用 10,如果您只使用内容可用"而没有其他任何内容,则应该是不允许的.工作很忙,我从来没有用 5 的优先级来测试这个,但它可能会有所帮助.
Another weird obscure thing to check is the priority in the notification frame data - link to Apple Documentation - command F for 'priority' for the details. When I was doing this my Ruby gem that built the notification was using 10 by default, which is supposedly not allowed if you are only using 'content-available' and nothing else. Things got busy at work and I never got to test this using a priority of 5, but it may help.
这篇关于APNS:didReceiveRemoteNotification:fetchCompletionHandler 在应用程序处于后台时没有运行(除非 xcode 正在调试)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!