user2971617 user2971617 - 6 months ago 9
Swift Question

Local notification is pushed but app doesn't open

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()
}

Answer

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/

Comments