5

I am trying to create a local notification for my app using Apple's UNUserNotificationCenter.

Here is my code:

let center = UNUserNotificationCenter.current()

let content = UNMutableNotificationContent()
content.title = task.name
content.body = task.notes
content.sound = UNNotificationSound.default()

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

let request = UNNotificationRequest(identifier: task.UUID, content: content, trigger: trigger)

center.add(request) { (error:Error?) in
   if let theError = error {
      print("Notification scheduling error: \(error)")
   }else{
      print("Notification was scheduled successfully")
   }
}

Access Request:

let center = UNUserNotificationCenter.current()

//request notification access
let options: UNAuthorizationOptions = [.alert, .sound]
center.requestAuthorization(options: options) { (granted, error) in
   if !granted {
      print("Notification access was denied")
   }
}

After 5 seconds the app makes the Default sound but does not show the alert content. I am trying both with the app in the foreground and in the background.

Matt Butler
  • 476
  • 6
  • 21

3 Answers3

15

I had the same problem.

If your content body is blank ie(content.body == "") then you won't get a notification even if you do have a title.

So the solution is to make sure that your content body is not an empty string.

365SplendidSuns
  • 3,175
  • 1
  • 21
  • 28
  • This was also my issue. – Joris Weimar Jul 12 '17 at 18:44
  • 1
    This is exactly whats happening with me on firebase push notification. If there is no body then it wont show up. Its weird. Do you know the reason behind it? – Nauman Aslam Jul 20 '17 at 19:29
  • 2
    Just an observation: local notifications in iOS 10.3 (10.x?) required the content body to be set for the alert to appear. But in iOS 11.x, the content body is not required. But you MUST specify one of the 3: title, subtitle or body. – ByteSlinger Jan 15 '18 at 18:02
2

Add this Protocoal UNUserNotificationCenterDelegate. and add this delegate into your controller. and dont forget to set delegate.

UNUserNotificationCenter.current().delegate = self

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("Notification being triggered")
        //You can either present alert ,sound or increase badge while the app is in foreground too with ios 10
        //to distinguish between notifications
        if notification.request.identifier == "yourrequestid"{
            completionHandler( [.alert,.sound,.badge])
        }
    }
Brijesh Shiroya
  • 3,323
  • 1
  • 13
  • 20
  • Thank you! completionHandler( [.alert,.sound,.badge]) is exactly what I was looking for. – Crono Oct 23 '18 at 15:38
0

Add this code into didFinishLaunchingWithOption in AppDelegate.swift file

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) 
{ (granted, error) in
// Enable or disable features based on authorization.
}
Brijesh Shiroya
  • 3,323
  • 1
  • 13
  • 20