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

查看:280
本文介绍了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方法(请注意completeHandler参数).

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天全站免登陆