Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving Notifications with App in background mode

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.

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

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

Thanks a lot,

Abras

like image 803
Abras Avatar asked Feb 08 '11 00:02

Abras


People also ask

Does an app need to be open for push notifications?

A mobile app uses “push notification” to send users a message that notifies them about something important without the need to open the app. The user does not need to do anything, as the app decides to push an alert to them, which can be a text message or an image.

What is background push notification?

Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device's system tray. A user tap on a notification opens the app launcher by default. Messages with both notification and data payload, when received in the background.

What is background fetch in iOS?

The Background Fetch API provides a method for managing downloads that may take a significant amount of time such as movies, audio files, and software.

What is silent push notification?

Silent notifications are notifications that do not make any sound or alert the user when it arrives, but it gets displayed only in the notification center/notification tray.


1 Answers

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) 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) 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) 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.

Hope that helps.

like image 185
Omar Raul Qazi Avatar answered Sep 19 '22 18:09

Omar Raul Qazi