0

I just did change for my notification for iOs 10 and others:

if #available(iOS 10.0, *) {
    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in

        let content = UNMutableNotificationContent()

        content.body = notifMessage!
        content.sound = UNNotificationSound.default()
        // Deliver the notification in five seconds.
        let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
        let request = UNNotificationRequest.init(identifier: "Upload", content: content, trigger: trigger)

        // Schedule the notification.
        let center = UNUserNotificationCenter.current()
        center.add(request)
    }
} else {
    let notification = UILocalNotification()
    notification.alertBody = notifMessage
    notification.fireDate = NSDate() as Date
    notification.soundName = UILocalNotificationDefaultSoundName
    UIApplication.shared.scheduleLocalNotification(notification)
}

When I run my apps on my device by connect it with the USB it works, but only when the app is in background, it doesn't work when:

  • i kill the app

  • when the app is displayed

Ben
  • 761
  • 1
  • 12
  • 35
  • Local notifications work even if the app is killed. Try to add the notification for much later than just 5 seconds, and make sure you change the identifier. Notification with same identifier so not work. – bhakti123 Feb 02 '17 at 13:37

2 Answers2

0

If you kill the app (by double tapping on home button and then swiping up), that not only terminates the app, but prohibits further background operation of the app (until the user fires it up again). You have to just press the home button and let the app be jettisoned via the normal memory recovery processes. Or you can, for testing purposes, programmatically crash app. But you cannot use springboard (the double tap of home button trick), because affects the permissible background modes of an app.

Regarding the notification when the app is displayed vs if the user taps on the notification vs user manually starts app having disregarding a notification, those are all conveyed to the app in different ways. See the Responding to Notifications and Events section of the UIApplicationDelegate documentation. Or see the App Programming Guide for iOS: Background Execution for general information about background operation.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
-1

There are a couple of errors in your code : 1. Title for the notification is missing. You have added the body and sound to the content but the title is missing. Title is a must and if you don't add the title the notification won't show.

content.title = "Some Title"

  1. Do not use init to initialise. These functions can be re-written as :

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

let request = UNNotificationRequest.init(identifier: "Upload", content: content, trigger: trigger)

  1. The identifier value is same. The identifier value needs to be different for every notification that you schedule. Notifications with same identifier do not appear.

let request = UNNotificationRequest(identifier: some_value, content: content, trigger: trigger)

  1. Trigger time. The time that you specifying in the trigger is for 5 seconds. Which may be less for you to close the app and test for the notification. Just to be on the safe side, make sure this value is at least 1 minute so that you can properly test out if it is working or not.

Hope this helps.

bhakti123
  • 833
  • 10
  • 22