UILocalNotification is deprecated at IOS10. Instead that you need to use UserNotifications framework. In this Swift 3 tutorial we can see the basics of the new framework, register an UNNotification and trigger it with custom sound.
IOS10, Xcode 8, Swift 3 tutorial.
Objective-C UNNotification tutorial - the same tutorial in that language
1. Create Single View Project with an UIButton
For the very first time to create a new Xcode project, and work with Storyboard, you need to read this Basic tutorial.
Now you need a ViewController and one UIButton on it. At your storyboard add Action method for your Button. So we are here
2. Requesting Authorization for User Interactions
The very first thing that you need, is the UserNotification.framework. Click the plus button at your target "Linked Frameworks and libraries".
If you want to send notifications (local and remote too), Requesting authorization is required. The first time your app requests authorization, the user is alerted and given an opportunity to deny or grant that authorization. After the initial request, the system remembers the user’s response and returns it during subsequent requests. So open your Appdelegate.swift file, import this Framework:
and put this code into your "didFinishLaunchingWithOptions launchOptions:"
3. Scheduling Local Notification
In ViewController.swift file import the Framework, and create a UNNotificationContent property.
Create your Notification in your setButton function. Set the icon Badge number, and create a request with this code. I used here just the apple 5 minutes tutorial, but I add and uncommented the method for using Date instead of TimeInterval.
4. Test and Set icon Badge to zero when App will foreground
Run the Application in Simulator. First you will see the automatic message, that you need to allow notifications. After that just click on the set Button, and immediately push Home button, or at Simulator reach this with this combo: cmd+Shift+H. You will see the Local Notification, which is awesome.
But if you go back again, without touch the set Button, you will see, that the icon badge stayed 1.
So you need to set the application badge to zero again. But when? At IOS10 the didReceiveLocalNotification:(UILocalNotification *)notification is deprecated. My solution is using applicationWillEnterForeground: function or applicationDidBecomeActive (at Swift this works perfectly)
In AppDelagate file set the badge number to 0, or if you would like to use other scenario, you can add -1 our check, how many notifications are. For more about UNNotificationCenter and methods click here.
Test again, and you will see, that the icon badge number disappear, after you opened the App again.
5. Using custom Sounds
You can use your own custom sound effect as a Notification sound, just you need to check the type and data format of the audio file, or convert it (check my tutorial about this). If you are ready with it, just put the file into your Xcode Project, and use it with this line of code:
Don't forget to delete your App from Simulator or device, before you test it again with the new custom sound.
At IOS 10.0.2 there is a Bug, so the custom sound doesn't play on the real device. There are lot of bug report at Apple, we are waiting for a solution.
The next part of the series: File Attachement in UNNotificationsContent