Can I use iOS local notifications if app is not running (like Clock app)?

182 Views Asked by At

I'd like to use iOS local UNNotifications even if my app is not running. The same way we use Alarm in Clock app. We can setup Alarm, then quit our Clock app but... sound and notification will run on time. I need the same type of behaviour.

Is it possible?

My code's excerpt:

import UIKit
import UserNotifications

class ViewController: UIViewController, UNUserNotificationCenterDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let content = UNMutableNotificationContent()
        content.title = "Our title"
        content.body = "A body of message"
        UNUserNotificationCenter.current().delegate = self
        content.sound = UNNotificationSound.default()

        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
        let request = UNNotificationRequest(identifier: "testID", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)  
    }
}
1

There are 1 best solutions below

2
On BEST ANSWER

Check this Apple Document About 30 seconds

Local and remote notifications can specify custom alert sounds to be played when the notification is delivered. You can package the audio data in an aiff, wav, or caf file. Because they are played by the system-sound facility, custom sounds must be in one of the following audio data formats:

Linear PCM

MA4 (IMA/ADPCM)

µLaw

aLaw

Place custom sound files in your app bundle or in the Library/Sounds folder of your app’s container directory. Custom sounds must be under 30 seconds when played. If a custom sound is over that limit, the default system sound is played instead.

Built-in Clock app made by Apple. have Special treatment (No way)