Timering

Purp

IOS10 - UserNotification.framework - UNNotification tutorial Obj-C

As you know, from IOS10, the UILocalNotification is deprecated. Instead that you need to use UserNotifications.framework. In this tutorial we can see the basics of the new framework, register an UNNotification and trigger it with custom sound.

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

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. 

single view button

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.m file, import this Framework:

#import <UserNotifications/UserNotifications.h>

and put this code into your "didFinishLaunchingWithOptions:(NSDictionary *)launchOptions"

I added the old IOS8 and IOS9 code version too, so you can see the changers. For example, to handle Badge, Sound the old code was "UIUserNotificationTypeSound | UIUserNotificationTypeBadge". Now you should use "UNAuthorizationOptionSound + UNAuthorizationOptionBadge". Of course we need to set the app icon badge to 0.

3. Scheduling Local Notification

In ViewController.h file import the Framework, and create a UNNotificationContent property. At your storyboard add Action mehto for your Button. So we are here

create notification and button unnotification ios10

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. 

unnotification send auto massage allow notifications

UNNotification notify locally

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:(UIApplication *)application function.

In AppDelagate.m 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, or with using UNNotificationCategory you manage more types too. For more about UNNotificationCenter and methods click here. 

07_appwillEnterForeground iconbadge

Test again, and you will see, that the icon badge number disappear, after you opened the App again. There is an other tip for you guys, if you want to call some method, to check, if the user just launched the app, or the app come foreground, you can check this, with an observer:

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.

NEXT

The next part of the series: The same tutorial in Swift

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!