【问题标题】:Local notification is pushed but app doesn't open本地通知已推送,但应用程序未打开
【发布时间】:2016-05-23 21:03:13
【问题描述】:

我已经在我的应用中完成了本地通知的实现。

当手机处于待机状态(黑屏但未关闭)时,我看到本地通知显示,我可以滑动并且我有两个按钮(接受和拒绝)。

问题:当我单击接受时,我可以在调试中看到该函数被触发并显示 print("babam!"),但应用程序保持关闭。 Normaly,应用程序应该打开!我停留在屏幕上,我需要在屏幕上滑动才能解锁并输入密码。也许吧,因为我必须解锁……但它看起来很奇怪。

您可以查看我如何声明本地通知。但

似乎是正确的
declineAction.authenticationRequired = false

所以任何帮助将不胜感激!

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)

就在我选择“接受”时触发的 func 下方

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

【问题讨论】:

    标签: swift uilocalnotification localnotification


    【解决方案1】:

    您应该将activationMode 值从acceptAction.activationMode = UIUserNotificationActivationMode.Background 更改为acceptAction.activationMode = UIUserNotificationActivationModeForeground

    在你的情况下;

    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
    

    了解更多信息; https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIUserNotificationAction_class/

    【讨论】:

    • 是的,谢谢它的工作。在哪些情况下,我们需要使用 .background 而不是 foreground ?
    • 我不知道这取决于您的应用需求。例如,当您收到新消息或电子邮件时,邮件应用程序会发送推送通知或whatsapp,如果您愿意,您可以通过阅读按钮(.foreground)打开应用程序,或者您可以使用取消按钮(.background)忽略消息,所以这取决于您的需求。
    猜你喜欢
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多