在后台模式下接收通知与App [英] Receiving Notifications with App in background mode

查看:246
本文介绍了在后台模式下接收通知与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.

有什么办法,我让这些通知,即使我的应用程序是处于暂停状态?我想运行只需3 code,每次我得到这个NowPlayingItemDidChange通知例如线。

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时,他们没有在前台应用程序暂停。有三种例外情况。你可以有code在后台执行,如果您的应用程序

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

一)播放音频。这意味着本身实际上是产生音频应用程序。我的理解是,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.

二)让您的应用程序来监视的位置。如果应用程序是使用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方法,您的应用程序将继续为时间背景中的有限的运行。如果你的用户会每隔10分钟左右进入你的应用程序一次,这可以正常工作。

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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