0

I have a server that successfully generates push notifications and sends them to devices. On the app side, if the user is already in the app or if the user enters the app from the notification I can easily 'find' the notification and run the necessary code to action the notification (app delegate gets invoked and the notification is stored in NSUserDefaults which I can then access throughout the app).

However, if the user has the app running in the background (for example, user was in the app, but then switched to another app), when the user reopens the app from the app icon, the app simply returns to the last state it was in and no code actually gets invoked (which is kind of what you expect).

What I am trying to figure out is how to either invoke AppDelegate again so that I can extract the notification before I return to the current state or how do I invoke some code so that I can intercept the notification.

Thoughts?

zevij
  • 2,416
  • 1
  • 23
  • 32

2 Answers2

0

If I understood your question correctly, you want to be notified when coming out of background state. How the appdelegate method

func applicationDidBecomeActive(application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
Lukas
  • 3,423
  • 2
  • 14
  • 26
  • Kind of. The situation is as follows: user launched the app following a notification. Because the app was effectively resumed, the AppDelegate does not get invoked. As it does not get invoked, the didReceiveRemoteNotification is not called and therefore the notification is ignored. So how would I intercept the notification if the user simply resumes the app? – zevij Sep 26 '15 at 18:08
  • Let me rephrase: The App Delegate gets invoked via applicationDidBecomeActive. I can and do check the 'badge number'. If it is not zero, I set the notification indicator in NSUserDefaults. However, after that, when the app actually resume, the view is already active (viewDidLoad does not run). So the code that checks for 'notifications' is not invoked. – zevij Sep 26 '15 at 18:18
  • how about calling it in viewWillApear – Lukas Sep 26 '15 at 18:20
  • meant to say viewWillAppear (which should be called everytime the view loads). No view method is actually called...app simply opens the view. Afterall, it was/is running in background so it 'has appeared' – zevij Sep 26 '15 at 18:22
  • you said "when the user reopens the app from the app icon, the app simply returns to the last state it was in and no code actually gets invoked" and yes viewWillAppear will be called when you change from background to active state – Lukas Sep 26 '15 at 18:25
  • App does not invoke viewWillAppear on return from background. Found confirmation to that here (http://stackoverflow.com/questions/5277940/why-does-viewwillappear-not-get-called-when-an-app-comes-back-from-the-backgroun). I guess I will need to add an observer as described in the link I found to get the desired behaviour. – zevij Sep 26 '15 at 18:30
  • Ok cool, I'll have to check that again. just learned something new :D thanks and good luck ;) – Lukas Sep 26 '15 at 18:32
0

Here is my solution.

  1. On 'App Resume', In AppDelegate, I check the badge and store push if badge value is greater than 0:

    func applicationDidBecomeActive(application: UIApplication) {            
            let installation = PFInstallation.currentInstallation()
        if installation.badge != 0 {
    
            NSUserDefaults.standardUserDefaults().setObject(true, forKey: "push")
    
            NSNotificationCenter.defaultCenter().postNotificationName("indicatePush", object: nil)
    
            installation.badge = 0
    
            installation.saveEventually()
        }}
    

    The call to NSNotificationCenter, invokes a registered method in my base class, where I set the push variable to true

  2. In the sub-class, on viewDidLoad I register two methods (you might only need one of them):

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "appplicationIsActive:", name: UIApplicationDidBecomeActiveNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: "applicationEnteredForeground:", name: UIApplicationWillEnterForegroundNotification, object: nil)

  3. Add the call to the 'pull from server' to one of the two methods. In my case:

    func appplicationIsActive (notification: NSNotification){
        refresh()    
    }
    

You're done!

zevij
  • 2,416
  • 1
  • 23
  • 32