30

So I been trying to add a notification to the new UNUserNotificationCenter, but I don't seem to get it.

My view controller has an action:

@IBAction func sendPressed(_ sender: AnyObject) {
    let content = UNMutableNotificationContent()

    content.title = "Hello"
    content.body = "What up?"
    content.sound = UNNotificationSound.default()

    // Deliver the notification in five seconds.
    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)

    // Schedule the notification.
    let center = UNUserNotificationCenter.current()
    center.add(request) { (error) in
        print(error)
    }
    print("should have been added")
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let center = UNUserNotificationCenter.current()
    center.requestAuthorization([.alert, .sound]) { (granted, error) in
    }
}

And I have a Notification Content Extension in the project as well, but it does not seem to be triggered at all, any ideas what I'm missing? I'm trying the example from the user documentation, but it's not telling me much more or I have missed it.

Here: https://developer.apple.com/reference/usernotifications/unmutablenotificationcontent

Also: https://developer.apple.com/reference/usernotificationsui https://developer.apple.com/reference/usernotifications

Edit:

So putting the app in the background did the trick.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Bjarte
  • 2,167
  • 5
  • 27
  • 37

5 Answers5

30

You need to register for Notification...I tried and this works.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization([.alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
        return true
    }

Edit: You dont need to put your app in background to present notification from iOS 10 onwards.

Use below callback to configure notification to present in foreground.

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

Here is a sample project.

LC 웃
  • 18,888
  • 9
  • 57
  • 72
  • Yes I have this, still no luck. Do you have a full app example so I can see if I missed anything? – Bjarte Jun 14 '16 at 11:42
  • @Bjarte in ios10..we can display notification while the app is in foreground as of android...see the sample project.... – LC 웃 Jun 15 '16 at 05:01
  • hi @anish I downloaded the app and ran on simulator. I don't see a notification? – rOrlig Oct 16 '16 at 19:25
  • Ahh..demo project was in Xcode 8 beta...I have updated example for the Xcode 8... – LC 웃 Nov 07 '16 at 07:12
  • how to set different date components or how to call local notfications multiple times? – ArgaPK Mar 05 '18 at 08:31
17

With Objective-C implemation:

I have wrote a Demo project here: iOS10AdaptationTips .

  1. import UserNotifications

    ///Notification become independent from Foundation
    @import UserNotifications;
    
  2. request authorization for localNotification

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {
                              if (!error) {
                                  NSLog(@"request authorization succeeded!");
                                  [self showAlert];
                              }
                          }];
    

    Request Authorization: enter image description here

  3. schedule localNotification

  4. update application icon badge number

        //        //Deliver the notification at 08:30 everyday
        //        NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
        //        dateComponents.hour = 8;
        //        dateComponents.minute = 30;
        //        UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES];
    
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
        content.title = [NSString localizedUserNotificationStringForKey:@"Elon said:" arguments:nil];
        content.body = [NSString localizedUserNotificationStringForKey:@"Hello Tom!Get up, let's play with Jerry!"
                                                             arguments:nil];
        content.sound = [UNNotificationSound defaultSound];
    
        /// 4. update application icon badge number
        content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
        // Deliver the notification in five seconds.
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
                                                      triggerWithTimeInterval:5.f repeats:NO];
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
                                                                              content:content trigger:trigger];
        /// 3. schedule localNotification
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (!error) {
                NSLog(@"add NotificationRequest succeeded!");
            }
        }];
    

then it will appear like this:

In Background : enter image description here Lock Screen:
enter image description here

If Repeat by default only show one enter image description here instead of show many on the lock screen on iOS9: enter image description here and also support 3D Touch automatically enter image description here

I write a Demo here: iOS10AdaptationTips .

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
ChenYilong
  • 8,543
  • 9
  • 56
  • 84
  • Hi , I ran you demo but it gives me exception **Assertion failure in -[UNTimeIntervalNotificationTrigger _initWithTimeInterval:repeats:]** while executing **UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5.f repeats:YES];**. – Ekta Padaliya Sep 05 '16 at 07:43
  • 1
    Why are you writing in Objective-C? – 4thSpace Sep 18 '16 at 05:53
  • 15
    Because he can ;) – CMash Sep 27 '16 at 21:24
  • Local notifications are not firing....Pl check [my post](http://stackoverflow.com/questions/40124268/local-notifications-are-not-firing-in-ios10) – Nazik Oct 19 '16 at 07:14
  • 1
    @EktaPadaliya Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'time interval must be at least 60 if repeating' – ChenYilong Oct 26 '16 at 04:20
  • @ElonChan Hi Elon, I have added the local notification you mentioned here for iOS 10. I have also checked that from iOS 10 we can show notification in foreground as well but I am not able to successfully implement that. could you please help me with that ? – Nik Nov 04 '16 at 11:07
  • HI, i tried u r demo. but it is not showing any notifications. – Ramakrishna Dec 14 '16 at 07:37
1

I solved my problem as follows (Firebase, Swift 3):

Find this method on your AppDelegate:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

Find this line:

completionHandler()

End set:

completionHandler([.alert,.sound,.badge])

notifications are not firing if you not pass your presentation options to completionHandler method.

ibrahimyilmaz
  • 2,317
  • 1
  • 24
  • 28
1

Here are a few steps:

  1. Make sure you have the permission. If not, use UNUserNotificationCenter.current().requestAuthorization to get that. Or follow the answer if you want to show the request pop up more than once.

  2. If you want to show the notification foreground, having to assign UNUserNotificationCenterDelegate to somewhere.

  3. Show me the code

    @IBAction func sendPressed(_ sender: AnyObject) {
        let content = UNMutableNotificationContent()
        content.title = "Hello"
        content.body = "What up?"
        content.sound = UNNotificationSound.default()
    
        let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
        let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)
    
        let center = UNUserNotificationCenter.current()
        center.add(request) { (error) in
            print(error)
        }
    }
    
    override func viewDidLoad(_ animated: Bool) {
        super.viewDidLoad(animated)
    
        // Assign the delegate
        UNUserNotificationCenter.current().delegate = self
    
        // Ask the permission
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization([.alert, .sound]) { (granted, error) in
            if granted {
                // do something
            }
        }
    }
    // Remember to add UNUserNotificationCenterDelegate to your view controller
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("Got the msg...")
        completionHandler([.badge, .sound, .alert])
    }
    
Allen
  • 2,979
  • 1
  • 29
  • 34
  • Cannot assign value of type 'MainScreen' to type 'UNUserNotificationCenterDelegate?', when using in view did load, UNUserNotificationCenter.current().delegate = self – Julian Silvestri Mar 25 '19 at 20:22
  • You might want to add UNUserNotificationCenterDelegate to your screen (ViewController) – Allen Mar 26 '19 at 10:00
0

I have made an implementation for Swift 3 which may help, you can check it here: https://stackoverflow.com/a/45381380/2296630

mourodrigo
  • 2,232
  • 28
  • 18