iOS receive Media Player Notifications when app is in background - ios

Probably a simple question - can an iOS app continue to receive media player notifications [NSNotifications] (i.e. MPMusicPlayerControllerNowPlayingItemDidChangeNotification) once it moves into background?
I'm specifically using an iPodMusicPlayer to play songs over my main app functionality, and id like to play about with implementing code under this notification after the app falls into background mode.
Not asking for code; just a point in the right direction re: background media notifications ;)

I have made some checks since i need the same.
Unfortunately I have seen that your app does not receive notification whilst in background.
However, I realized that it does receive the entire notification queue once resumed.
The problem is that notification are received all together giving you no information about the time and date they were fired up.
Of course, since you receive them all at once, even if you try to save the nowPlayingItem after the MPMusicPlayerControllerNowPlayingItemDidChangeNotification, you will only see the current item when your app becomes active again, and nothing more.
Unfortunately, I think we cannot do anything more, since Apple doc also says
The notification has no userInfo dictionary
meaning that you cannot get any info out of the notification.

Related

Can an app know when user is driving from iOS 11 Do Not Disturb While Driving feature?

In iOS 11 Apple blocks push notifications by giving a notification that you are driving and notifications are blocked.I am curious can we extract that information from iOS and use it in my app?
EDIT: I have already a mechanism with me to detect driving mode but I want to know whether we can access that data or not and want to know what else is there in that data(if that is available).So please dont post links leading to Driving mode detection algos.
Most of the Apps Performing such operations rely on data from Accelerometer. All smartphones now a days, including iOS has builtin Accelerometer sensor in them.
The idea to predict whether a person is driving or not is to infer speed of the user movement from the data provided by this sensor and perform operation based on that.
All SDKs provide functions that can use this data. Hence, it will be available for you also.
There is no public API that manage Do Not Disturb Driving, if you want to know user activity you can use CMMotionActivity:
var automotive: Bool
A Boolean indicating whether the device is in an automobile.
On devices that support motion, you can use a CMMotionActivityManager
object to request updates when the current type of motion changes.
When a change occurs, the update information is packaged into a
CMMotionActivity object and sent to your app.
All push notification are managed by the system, if they are blocked you can not access to their data. Maybe if you need some notification to do something in your app, you can use Silent Push Notification:
Configuring a Silent Notification
The aps dictionary can also contain the content-available property.
The content- available property with a value of 1 lets the remote
notification act as a silent notification. When a silent notification
arrives, iOS wakes up your app in the background so that you can get
new data from your server or do background information processing.
Users aren’t told about the new or changed information that results
from a silent notification, but they can find out about it the next
time they open your app.
For a silent notification, take care to ensure there is no alert,
sound, or badge payload in the aps dictionary. If you don’t follow
this guidance, the incorrectly-configured notification might be
throttled and not delivered to the app in the background, and instead
of being silent is displayed to the user
But maybe this kind of notifications are blocked too:
Do Not Disturb While Driving feature and it will basically turn off
your phone without actually turning it off, so no notifications of any
kind will be able to get through.

Back to iOS application automatically when it's suspending in background.

I have a question,
I work on a Swift 3 project with Xcode.
My goal is to send an alert sms, 20 secondes after receiving a local notification. when I'm in application, every things work well, but when my app is in background, it can do other tasks, like print some character, or play music, or send a text to a socket server, but it can't send the sms.
I think, when I receive an alert, if I could back to my application automatically, then I can send an alert sms, that's why I'm doing this stupide idea and looking for a way to know how I can wake up automatically my application and go back to the first View Controller Screen when I receive a Local Notification?
Maybe there is someone who knows a method to return to the iOS application automatically?
When your app is launched into the background—usually to handle some type of background event—the launch cycle changes slightly as given in the picture below. The main difference is that instead of your app being made active, it enters the background state to handle the event and may be suspended at some point after that. When launching into the background, the system still loads your app’s user interface files but it does not display the app’s window.
Reference - https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/StrategiesforHandlingAppStateTransitions/StrategiesforHandlingAppStateTransitions.html
Bringing your app from background to foreground is not possible. And even if you are able to do so, App store will reject it.
Hope it helps!

Setting iOS app badge while in the background after receiving a remote notification

I've seen a bunch of questions about getting the correct iOS app badge number in response to a remote notification (like this, this, and this). They all hinge on realizing that the OS will badge the app with the number in the payload before your app does anything. I get that, but my question is a bit different.
Assume the following series of events for an app that's enabled the "remote-notification" background mode (and otherwise properly configured for remote notifications):
The server sends a remote notification. The payload says the badge number should be 7.
The OS gets this, badges the app with "7", shows the alert/banner to the user, and launches/wakes my app into the background to start processing data.
Shortly after running, the app realizes the server is wrong and the correct badge number should be 3.
Maybe no big deal if the user comes into the app right away, but I'd like the badge to be correct if they don't. Can I set the badge number from within application:didReceiveRemoteNotification:fetchCompletionHandler: while executing in the background?
This article suggests you can change the badge if you asked for the "background fetch" background mode and the OS granted your wish. Any reason it shouldn't work if you're backgrounded to handle a notification?
For anyone who stumbles upon this later: I managed to get everything setup to actually try this out. Yes, you can mess with the app icon badge while in the background to handle a remote notification. At least when it comes to the badge, there's nothing special or restrictive about executing in the background due to a notification as opposed to any other background mode.

how to start my App in a particular time of the Day in Swift iOS

I have an App built on Swift, I want this App to start every day at some particular time. Logically its like Calendar notification, which gives notification in that particular window whatever we set.
Does is the same scenario is possible with an App in iOS Swift.
What you can probably do is to create a local notification, but this is not opening your app. A local notification is just a way to show a notification on your iPhone and then, if the user taps, it's opening your app.
See more here: https://www.codebeaulieu.com/49/How-to-add-local-notifications-to-your-app
I am not sure what you want to do, you cannot force your application upon the user without the user's consent. What you can do is schedule a local notification so the user knows when to open your app like jomafer proposed already. Also possible is to wake up the app to do stuff in the background:
https://developer.apple.com/library/prerelease/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
For example a silent push notification that will trigger some code, or a scheduled background download.
NONE OF THESE METHODS ARE 100% RELIABLE THOUGH!

Cancel Local Notifications after app is unused for a certain period of time

I am wondering if it's possible to do what the titles says. I have an application that has a refill reminder to refill your prescription drug via local notifications. I have seen that some apps (pill reminder apps mostly) push a notification if you have not taken your pill, or have not answered back to that notification, and was wondering if I can do the same if a user doesn't open/interact with the app after a certain period of time.
I have not began implementation but have thought about this thoroughly. What I am thinking of doing is having some sort of flag when the app is opened that removes that local notification and sets a new one once the app has gone in the background/inactive. The local notification would be set to three months from when the app has gone in the background/inactive. The question then becomes, how do I handle canceling all notifications after this notification has been received, regardless of whether the user opens the app at that notification or not?
If the user opens the app on that notification, I can have a check the method application:didReceiveLocalNotification and then handle the case where that local notification has been set and then use [[UIApplication sharedApplication] cancelAllLocalNotifications]
But if the user does not tap or open the app, how can I check and cancel all local notifications?
Sorry if this is a bit long or worded weirdly (sorry not good with words and explaining things). Let me know if you need more info or better explanation. Thanks in advance!
If I understand your question correctly, you want to know how to keep notifications from repeating when the user does not respond to a notification by opening your app.
You might consider configuring your local notification not to repeat. Instead, you might reschedule notification batches each time the application is launched.
Alternatively, if your application has a server-side component, you can use push notifications on iOS 7+ to wake your app, briefly. There is no equivalent to this behavior using UILocalNotification.

Resources