When you receive the notification you can look up the playbackState property of the MPMusicPlayerController.
As for executing a function 10 seconds before the song ends, you could set up a timer to execute in the future based on the length of the song minus 10 seconds, you would need to cancel that timer if the user changes the playbackState
or selects a different song.