2

I'm using remote notifications to wake up my app and conditionally trigger a UILocalNotification.

I've found that if this notification is triggered while the app is not running, all I get is the notification sound (no alert/notification). It works as expected if the app is in the background.

Registering for remote notifications in AppDelegate:

let settings = UIUserNotificationSettings(forTypes: (.Badge | .Sound | .Alert), categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()

Creating/Firing of UILocalNotification (as a result of didReceiveRemoteNotification)

var notification = UILocalNotification()
notification.alertTitle = "My App"
notification.alertBody = "Hello World!"
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().presentLocalNotificationNow(notification)

As far as I can tell from the documentation, notification behavior when the app is in the background and simply not running should be the same..

When the system delivers a local notification, several things can happen, depending on the app state and the type of notification. If the app is not frontmost and visible, the system displays the alert message, badges the app, and plays a sound—whatever is specified in the notification. If the notification is an alert and the user taps the action button (or, if the device is locked, drags open the action slider), the app is woken up or launched.

Source: UILocalNotification Documentation

Insight into what's going on here is much appreciated!

2
  • Run the Settings app and go to the page for your app. Check the notification settings for your app.
    – rmaddy
    Jun 19, 2015 at 23:56
  • I've verified notification settings for the app are enabled. As I mentioned, it all works as expected when the app is in the background, but not when I explicitly close it (by swiping it away)
    – Rhinon
    Jun 19, 2015 at 23:59

1 Answer 1

1

The problem has nothing to do with the display of local notifications.

As you said in your question, you use remote notifications to TRIGGER opening your app and then run code to conditionally show a local notification.

The reason why this is not working is:

Your code to show a local notification is NOT run from remote notifications after the user has force closed the app (by swipe up)!

You can find more on this in the developer forums here: https://devforums.apple.com/message/873265#873265 (dev account login requiered)

Also keep in mind that if you kill your app from the app switcher (i.e. swiping up to kill the app) then the OS will never relaunch the app regardless of push notification or background fetch. In this case the user has to manually relaunch the app once and then from that point forward the background activities will be invoked.

1
  • Thanks, this does appear to be the case. It seems I should move my decision point on what notification to show to my web server to ensure reliable notifications. More info here: stackoverflow.com/questions/19068762/…
    – Rhinon
    Jun 22, 2015 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.