Adding Sound To a Notification

Have you ever received a push notification from the ESPN app and it displays the final score with ESPN’s “da-na-na, da-na-na” sound? Have you ever ruminated on said notification and thought I wish I could do that for my own app?

Ponder no further. Ill show you how to make a notification with a custom sound.

Step one — convert to .m4r using iTunes

If you have an mp3 or wav file you can skip step one.

What you need to do: Add song to itunes > convert to AAC > find in finder > rename file extension from .m4a to .m4r

.AAC is what we want. Here is more information on how to convert a file to AAC in iTunes https://support.apple.com/en-us/HT204310

Step one (alternative way )— convert to .m4r using Garageband

Open the file in Garageband. Edit to the exact sound you desire. Apple requires that all notification sounds must be 30 seconds or less.

Then click Share > Song to iTunes…

Within iTunes File > Show in Finder. Then rename the .m4a to .m4r click ok. Drag file to desktop for the next step.

Editing a sound in Garageband

Step two — convert sound to caf

We need to take our .m4r and convert it to a .caf file. Fun fact .caff stands for Core Audio File Format. Enter this afconvert command which already exists in terminal

afconvert -d LEI16 -f caff LEI16@44100 -c 1 input_file.xxx output_file.caf

So for example if I have the file “ANewRecord.m4r” (no spaces) I’d enter the command:

afconvert -f caff -d LEI16@44100 -c 1 ANewRecord.m4r newRecordm4r.caf

Step three — drag and drop

Your new .caf file into your project’s Supporting Files folder. MAKE SURE your app is selected as the target.

Step four-copy paste

This block of code into your AppDelegate.m file. As of iOS 8 we need to ask the user for permission to send them notifications.

//within applicationDidFinishLaunchingWithOptions 
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
    if ([UIApplication instancesRespondToSelector: @selector(registerUserNotificationSettings:)])
{
[application registerUserNotificationSettings:[UIUserNotificationSettings 
settingsForTypes:UIUserNotificationTypeAlert| UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
  }
}

Step 5 — Create notification

All local notifications are of type UILocalNotification. I encourage you to look into it in your spare time, as it has several nifty scheduling features already built-in. One can create a notification to launch 10 seconds from now, or even 10 hours from now. Pretty sweet!

What were gonna do:

  • Create an instance of UILocalNotification
  • Set its alertBody to inputted text
  • Set its soundName — to the “filename.caf” we created before
  • Display the notification now
//AppDelegate.m   //within applicationDidEnterBackground 
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @”Hey fancy that, its a new record!”;
notification.soundName = @”newRecordm4r.caf”;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];

Thats it! Now lets put it all together!

References:

https://stackoverflow.com/questions/7170475/choose-custom-sound-for-local-notifications

Apple’s Documentation: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html

http://www.appcoda.com/ios-programming-local-notification-tutorial/