在后台模式下使用

在后台模式下使用

本文介绍了在后台模式下使用 App 接收通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,可以跟踪用户在 iPod 应用程序中所做的一切.为此,我向 NSNotificationCenter 添加了一些观察者,例如 MPMusicPlayerControllerNowPlayingItemDidChangeNotification.但我的问题是,我只在我的应用程序在前台时收到这些通知,如果它在后台,系统会将通知添加到队列中,然后在我的应用程序下一次激活时,它会将它发送给我.我对这个队列不感兴趣,因为我想接收实时通知.

I have an app, that will keep track of everything the user do in the iPod app. To do this, I added few observers to NSNotificationCenter, like MPMusicPlayerControllerNowPlayingItemDidChangeNotification. But my problem is, I only get those notifications when my app is in the foreground, if its in the background, the system add the notification to a queue, and then the next time my app becomes active it delivers it to me. I have no interest in this queue, since I want to receive real-time notifications.

即使我的应用程序处于挂起状态,我有什么办法可以收到这些通知吗?例如,每次收到 NowPlayingItemDidChange 通知时,我只想运行 3 行代码.

Is there any way for me to get those notifications even if my app is in suspended state? I want to run just 3 lines of code everytime I get this NowPlayingItemDidChange notifications for example.

这里是我添加观察者的地方.

Here is where I add the observer.

MPMusicPlayerController *iPodMediaPlayer = [MPMusicPlayerController iPodMusicPlayer];



NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

     [notificationCenter addObserver: self selector: @selector(handle_NowPlayingItemChanged:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification

                                    object:iPodMediaPlayer];


 [iPodMediaPlayer beginGeneratingPlaybackNotifications];

另外,如果我向观察者添加另一种对象而不是 iPodMediaPlayer,观察者将不会调用该方法.

Also, if I add another kind of object to the observer instead of iPodMediaPlayer, the observer won't call the method.

非常感谢,

阿布拉斯

推荐答案

iOS 应用程序不在前台时会被挂起.此规则有三个例外.如果您的应用程序是

iOS applications are suspended when they are not in the foreground. There are three exceptions to this rule. You can have code execute in the background if your application is

a) 播放音频.这意味着应用程序本身实际上正在生成音频.我的理解是 MPMediaPlayerController iPodMusicPlayer 对象只控制外部 iPod 进程的播放,而不是从应用程序本身播放音频.如果您调用 applicationMusicPlayer 而不是 iPodMusicPlayer 并在您的应用程序 Info.plist 中设置适当的背景标志,也许您会取得一些成功.这似乎是让您的应用程序运行的最合法方式,但您无法从 iPod 应用程序控制 iPod 播放,只能通过您的应用程序和系统音频控件来控制.

a) Playing audio. This means the application itself is actually generating audio. My understanding is that the MPMediaPlayerController iPodMusicPlayer object only controls the playback of the external iPod process, rather than playing audio from the app itself. Perhaps you could have some success if you called applicationMusicPlayer instead of iPodMusicPlayer and set the appropriate background flags in your applications Info.plist. This seems like the most legitimate way to get your application to work, but you wouldn't be able to control iPod playback from the iPod app, only your app and the system audio controls.

b) 让您的应用监控位置.如果应用程序正在使用 GPS,它可以继续在后台执行.这样做的缺点是 GPS 会耗尽电池电量,并且用户可能会因为您请求他们的位置而感到害怕.

b) Get your app to monitor the location. If the app is using the GPS it can continue to execute in the background. Downside to this is that the GPS will drain battery, and users might be creeped out that you're requesting their location.

c) 向 UIApplication 请求额外的时间.如果您使用 UIApplication 的 beginBackgroundTask 方法,您的应用程序将在后台继续运行有限的时间.如果您的用户每十分钟左右进入您的应用程序一次,这也可以奏效.

c) Ask UIApplication for extra time. If you use UIApplication's beginBackgroundTask method, your application will continue to run for a finite amount of time in the background. If your users are going to come into your application once every ten minutes or so, this could work as well.

希望有所帮助.

这篇关于在后台模式下使用 App 接收通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:03