Timering

Purp

IOS10 - UserNotification.framework - UNNotification tutorial Swift

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.

Steps:

  1. Create Single View Project with an UIButton
  2. Requesting Authorization for User Interactions
  3. Scheduling Local Notification
  4. Test and Set icon Badge to zero when App will foreground
  5. Using custom Sounds - convert to corect type and data format

Part of the series

 Convert audio files for UNNotification custom sounds

Objective-C UNNotification tutorial - the same tutorial in that language

Video

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

Button settings user notification Swift

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"

add user notification framework to the project

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:

import UserNotifications

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. 

Allow notification user notification swift

UNNotification notify simulator swift

But if you go back again, without touch the set Button, you will see, that the icon badge stayed 1.

UNNotification icon badge stay 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. 

didbecame active swift app icon badge number set zero

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. 

NEXT

The next part of the series: File Attachement in UNNotificationsContent

Share & Follow

If you would like to be notified of my new solutions, please subscribe to my Newsletter or my youtube channel and/ or follow me on Facebook or Twitter! Check Footer section for these opportunities!