本地通知已推送,但应用程序未打开

Posted

技术标签:

【中文标题】本地通知已推送,但应用程序未打开【英文标题】: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()
    

【问题讨论】:

【参考方案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)忽略消息,所以这取决于您的需求。

以上是关于本地通知已推送,但应用程序未打开的主要内容,如果未能解决你的问题,请参考以下文章

已安排本地通知,但未在挂起和未运行模式下传递

iOS“本地”推送通知[关闭]

如何处理未打开的推送通知(iOS、Swift)

当应用程序未运行时,通过静默推送通知在后台更新本地数据库

本地通知和推送通知

应用程序未运行时的本地通知