简体   繁体   English

在后台模式下使用App接收通知

[英]Receiving Notifications with App in background mode

I have an app, that will keep track of everything the user do in the iPod app. 我有一个应用程序,可以跟踪用户在iPod应用程序中所做的一切。 To do this, I added few observers to NSNotificationCenter, like MPMusicPlayerControllerNowPlayingItemDidChangeNotification. 为此,我向NSNotificationCenter添加了一些观察者,例如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. 我对此队列没有兴趣,因为我想接收实时通知。

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. 例如,每次我收到此NowPlayingItemDidChange通知时,我只想运行3行代码。

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];

Also, if I add another kind of object to the observer instead of iPodMediaPlayer, the observer won't call the method. 另外,如果我向观察者而不是iPodMediaPlayer添加另一种对象,则观察者将不会调用该方法。

Thanks a lot, 非常感谢,

Abras 亚伯拉

iOS applications are suspended when they are not in the foreground. iOS应用程序不在前台时将被挂起。 There are three exceptions to this rule. 此规则有三个例外。 You can have code execute in the background if your application is 如果您的应用程序是

a) Playing audio. a)播放音频。 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. 我的理解是MPMediaPlayerController iPodMusicPlayer对象仅控制外部iPod进程的播放,而不播放应用程序本身的音频。 Perhaps you could have some success if you called applicationMusicPlayer instead of iPodMusicPlayer and set the appropriate background flags in your applications Info.plist. 如果您调用applicationMusicPlayer而不是iPodMusicPlayer并在应用程序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. 这似乎是使应用程序正常工作的最合法方法,但是您将无法从iPod应用程序控制iPod的播放,只能从应用程序和系统音频控件中进行控制。

b) Get your app to monitor the location. b)获取您的应用程序以监视位置。 If the app is using the GPS it can continue to execute in the background. 如果应用使用的是GPS,则可以继续在后台执行。 Downside to this is that the GPS will drain battery, and users might be creeped out that you're requesting their location. 不利的一面是GPS会耗尽电池电量,并且用户可能会因为您要求他们的位置而感到无所适从。

c) Ask UIApplication for extra time. c)要求UIApplication额外的时间。 If you use UIApplication's beginBackgroundTask method, your application will continue to run for a finite amount of time in the background. 如果使用UIApplication的beginBackgroundTask方法,则应用程序将在后台继续运行有限的时间。 If your users are going to come into your application once every ten minutes or so, this could work as well. 如果您的用户每隔十分钟左右进入您的应用程序一次,那么这也可能会起作用。

Hope that helps. 希望能有所帮助。

As I explained here iOS receive Media Player Notifications when app is in background , there seems no way to get notifications from iPodMusicPlayer. 正如我在此说明的, iOS在后台运行时会收到Media Player通知 ,似乎无法从iPodMusicPlayer获取通知。

About Omar Raul Qazi answer: 关于Omar Raul Qazi的答案:

a) i tried and I had no success. a)我尝试过,但没有成功。 The music kept going down when pressing home button. 按下主屏幕按钮时,音乐一直在下降。 I think this background flag only works for normal AudioSessions and not for MPMusicPlayer... 我认为此背景标志仅适用于普通AudioSessions,不适用于MPMusicPlayer ...

b) I am not sure this would work and I don't think Apple would like it when looking for approval b)我不确定这是否行得通,并且我不认为苹果在寻求批准时会喜欢它

c) You can run in background only synchronous task. c)您可以在仅后台同步任务中运行。 You cannot wait there for a notification. 您不能在那里等待通知。 Am I wrong? 我错了吗?

Multitasking in iOS is currently a very restricted one. 目前,iOS中的多任务处理非常受限制。 You may start a background task using beginBackgroundTaskWithExpirationHandler: method (of an UIApplication object), but it is intended to finish a finite-length task before going suspended. 您可以使用(UIApplication对象的) beginBackgroundTaskWithExpirationHandler:方法启动后台任务,但是该任务旨在在挂起之前完成有限长度的任务。 All background tasks may expire (and get terminated) before it finishes its job. 在完成工作之前,所有后台任务可能会过期(并终止)。 You can check how much longer your application can run by checking backgroundTimeRemaining property of the application object. 您可以通过检查应用程序对象的backgroundTimeRemaining属性来检查应用程序可以运行多长时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM