Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local Notifications make sound but do not display (Swift)

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.

like image 526
Matt Butler Avatar asked Feb 24 '17 05:02

Matt Butler


People also ask

How do I enable notifications in SwiftUI?

Step 1: In your SwiftUI app, create a new class called LocalNotificationManager that adopts the @ObservableObject. Adopting this protocol isn’t a must but can be useful if you want your SwiftUI view to react when a local notifications gets fired.

How to send a notification after 5 seconds in Swift?

The Swift code for sending a local notification after 5 seconds is as follows: Basically, the above code simply instantiates a UNMutableNotificationContent, which is the content of your notification message, so we need to supply it with the title, subtitle and the sound.

How to send local notifications in your app?

The local notification is a handy way for you to send notifications right away from the app to the users without having to do it from the server side. To send local notifications in your app, all you need to do is to make use of the UserNotifications framework. First of all, you’ll need to import the UserNotifications as below:

What are the different types of notifications in iOS?

There are two types of notifications in iOS app, the local and remote notifications. The local notification is a handy way for you to send notifications right away from the app to the users without having to do it from the server side. To send local notifications in your app, all you need to do is to make use of the UserNotifications framework.


2 Answers

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.

like image 70
365SplendidSuns Avatar answered Oct 18 '22 22:10

365SplendidSuns


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])
        }
    }
like image 27
Brijesh Shiroya Avatar answered Oct 18 '22 20:10

Brijesh Shiroya