iOS 后台任务/推送通知 [英] iOS Background Task/Push Notification

查看:42
本文介绍了iOS 后台任务/推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个需要在应用程序处于后台(或屏幕关闭)时朗读数据的应用程序.口语部分通过AVSpeechSynthesizer完成.

I am building an app that needs to speak data while the app is in the background (or screen is off). The speaking part is done via AVSpeechSynthesizer.

我的 Android 应用启动了一个服务来监听数据,只要它需要说些什么,我就会说出来,然后就完成了.现在,iOS 不允许从我阅读的内容中执行此类后台任务.壁橱里的东西被称为执行有限长度任务",它的时间限制最多为 10 分钟.所以这对我不起作用.相反,我似乎需要使用推送通知.

My Android app launches a service that listens for the data and whenever it needs to say something, I say it and its done. Now iOS doesn't allow these kind background tasks from what I read. The closet thing is called 'Executing Finite-Length Tasks' which looks like has a time limit of 10 mins max. So this will not work for me. Instead it looks like I need to use Push Notifications.

查看推送通知的文档,如果我理解正确,如果我的应用程序在前台,那么我的应用程序会立即收到推送通知.而如果我的应用程序在后台,则会设置一个通知(通知中心),一旦用户点击通知,我的应用程序就会使用有效负载启动.

Looking at the documentation for Push Notifications, if I understand it correctly, if my app is in the Foreground, then my app receives the Push Notification instantly. While if my app is in the Background, a notification is set (notification center), and once the user hits the notification, my app launches with the payload.

这里的问题是,有没有办法让推送通知立即唤醒我的应用程序,以便我可以说一些信息?

The question here is, is there anyway to make a push notification wake my app immediately so I can speak some info?

或者在这种情况下应该使用哪些其他替代方法?

Or what are some of the other alternative approaches that should be used in this case?

我的另一个想法是为每个用户实现某种 mp3 流,这将流式传输我可以在后台播放的音频.但这对于我想要完成的事情来说似乎太过分了?

My other idea would be to implement some sort of mp3 stream per user, that would stream audio which I could play in the background. But this seems excessive for what I am trying to accomplish?

推荐答案

我不确定您是否能够从后台调用语音合成,但是您可以在推送到达时通知您的应用程序.诀窍是实现

I'm not sure if you'll be able to invoke speech synthesis from the background, but you can have you app notified when the push arrives. The trick is to implement the

- (void) application: (RMApp *) application didReceiveRemoteNotification: (NSDictionary *) userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult result)) completionHandler

UIApplicationDelegate 方法(注意completionHandler 参数).

UIApplicationDelegate method (note the completionHandler parameter).

您还必须设置远程通知后台模式功能.

并且您必须在推送通知负载中设置 content-available.

And you must set content-available in your push notification payload.

在此处找到有关这些步骤的体面文章:
http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/

A decent writeup on these steps found here:
http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/

这篇关于iOS 后台任务/推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆