Notification with sound on Swift

Asked

Viewed 212 times

1

I’m having a problem making an app similar to an alarm. What happens is that I can’t find any way for the user to select a song in their library and use it when playing a notification. This is possible at Swift?

What I implemented was an excerpt that carries an internal sound file, but I’d like to get a song from the library:

let content = UNMutableNotificationContent()
content.title = "Late wake up call"
content.body = "The early bird catches the worm, but the second mouse gets the cheese."
content.categoryIdentifier = "alarm" 

content.sound = UNNotificationSound.init(named: "my-music.mp3");

Another issue is that I did not find something related to the iOS alarm, in which it opens a screen with a STOP or FORWARD button even being in the background. On Swift this is possible?

1 answer

0


The sounds of notifications (local or remote) are intended to be an alert, so there are restrictions:

  • Audio file in aiff, wav, or Caf (in Linear PCM, MA4 (IMA/ADPCM), µLaw or aLaw encodings);
  • Be in the app Bundle or in the container’s Library/Sounds folder;
  • Have less than 30 seconds;

Documentation: Local and Remote Notification Programming Guide


About the iOS alarm, there is still no possibility to display content on the locked screen as Apple does.

All alarm apps I tested nay let iOS block, just erase the screen. If you want to follow this path, take a look at the properties:

  • UIApplication > isIdleTimerDisabled;
  • UIDevice > proximityMonitoringEnabled;
  • Thank you very much for the information, just a question. There is an app called "Alarm Clock for me", it lets me upload a song from my library as a ringtone. But this only works when the app is open, in case I leave in the background the music does not work. So the process it does is to call a specific method that plays the music, through a Scheduler?

  • This will only work if the app is running, if it is in the background does not work. If you want you can even use the notification to schedule, when you receive the notification you play the song.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.