0

I already finish the implementation of local notification in my app.

When the phone is in standby (black screen but not closed), i see the local notification displays, i could swipe and i have two buttons ( Accept & Decline ).

The problem : When i click on Accept, i can see on the debug that function is triggered with print("babam!") displayed, but the app stay closed. Normaly, the app should open ! I stay on the screen, where i need to swipe to unlock and given my password. Maybe , because i have to unlock... but it seems weird.

You could check how i declare my local notification. But it seems correct with

declineAction.authenticationRequired = false

So any help will be appreciated !

let acceptAction = UIMutableUserNotificationAction()
        acceptAction.identifier = "Accept"
        acceptAction.title = "Accept"
        acceptAction.activationMode = UIUserNotificationActivationMode.Background
        acceptAction.destructive = false
        acceptAction.authenticationRequired = false

        let declineAction = UIMutableUserNotificationAction()
        declineAction.identifier = "Decline"
        declineAction.title = "Decline"
        declineAction.activationMode = UIUserNotificationActivationMode.Background
        declineAction.destructive = false
        declineAction.authenticationRequired = false


        let category = UIMutableUserNotificationCategory()
        category.identifier = "invite"
        category.setActions([acceptAction, declineAction], forContext: UIUserNotificationActionContext.Default)
        let categories = NSSet(array: [category])

        let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: categories as? Set<UIUserNotificationCategory>)
        UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)

Just below func which is triggered when i choose "accept"

 func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
        NSLog("Handle identifier : \(identifier)")
        // Must be called when finished
        if identifier == "Accept"{
        print("babam!!!")


            var storyboard = UIStoryboard(name: "Main", bundle: nil)

            var destinationController = storyboard.instantiateViewControllerWithIdentifier("gosondage1") as? Sondage

            var frontNavigationController = UINavigationController(rootViewController: destinationController!)

            var rearViewController = storyboard.instantiateViewControllerWithIdentifier("MenuController") as? MenuController

            var mainRevealController = SWRevealViewController()

            mainRevealController.rearViewController = rearViewController
            mainRevealController.frontViewController = frontNavigationController
            self.window!.rootViewController = mainRevealController
            self.window?.makeKeyAndVisible()


             }
        completionHandler()
    }
user2971617
  • 63
  • 1
  • 9

1 Answers1

0

You should change the activationMode value from acceptAction.activationMode = UIUserNotificationActivationMode.Background to acceptAction.activationMode = UIUserNotificationActivationModeForeground

In your case;

acceptAction = UIMutableUserNotificationAction()
        acceptAction.identifier = "Accept"
        acceptAction.title = "Accept"
        acceptAction.activationMode = UIUserNotificationActivationModeForeground
        acceptAction.destructive = false
        acceptAction.authenticationRequired = false

        let declineAction = UIMutableUserNotificationAction()
        declineAction.identifier = "Decline"
        declineAction.title = "Decline"
        declineAction.activationMode = UIUserNotificationActivationMode.Background
        declineAction.destructive = false
        declineAction.authenticationRequired = false

for more information ; https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIUserNotificationAction_class/

emresancaktar
  • 1,507
  • 17
  • 25
  • Yes, thanks it's working. In which cases, do we need to use .background instead of foreground ? – user2971617 May 23 '16 at 22:21
  • I dont know it depends on your app needs. For example mail app sends push notifications or whatsapp when you receieve a new message or email and if you want you can open the app via read button(.foreground) or you can ignore the message with cancel button(.background) so it depends on your needs. – emresancaktar May 24 '16 at 07:30