Local Notifications make sound but do not display (Swift)
Asked Answered
R

3

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.

Rancidity answered 24/2, 2017 at 5:7 Comment(6)
are you register it in didFinishLaunchingWithOptions method?Sachi
@Sachi Yes, I have registered it in the app delegateRancidity
can you show that code ?Sachi
@MattButler I have the same problem, do you found any solution?Amulet
@Carol Yes, be sure that your local notification has title and message content. Without the message the notification does not show.Rancidity
I have also face this issue, i have received local notification but after 5 seconds automatically dismiss the notification popup, if any body give the suggestion to stay the popup more than 5 seconds?Marmolada
R
16

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.

Redpencil answered 19/5, 2017 at 10:22 Comment(3)
This was also my issue.Fussbudget
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?Stander
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.Multipara
R
3

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])
        }
    }
Retinite answered 24/2, 2017 at 6:3 Comment(1)
Thank you! completionHandler( [.alert,.sound,.badge]) is exactly what I was looking for.Fruge
R
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.
}
Retinite answered 24/2, 2017 at 5:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.