【问题标题】:iOS locale notification pop-up doesn't appeariOS 区域设置通知弹出窗口未出现
【发布时间】:2015-07-30 17:44:57
【问题描述】:

我是 iOS 编程的新手,在我的应用程序中,我有一个 UILocaleNotification,它必须在某个时间出现。

所以首先我设置参数:

let notificationSettings: UIUserNotificationSettings! = UIApplication.sharedApplication().currentUserNotificationSettings()

    if (notificationSettings.types == UIUserNotificationType.None) {
        var notificationTypes: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Sound

        // Bouton d'envoi
        var sendAction = UIMutableUserNotificationAction()
        sendAction.identifier = "send"
        sendAction.title = "Send"
        sendAction.activationMode = UIUserNotificationActivationMode.Foreground
        sendAction.destructive = false
        sendAction.authenticationRequired = true

        // Bouton de non-envoi
        var dontSendAction = UIMutableUserNotificationAction()
        dontSendAction.identifier = "dontsend"
        dontSendAction.title = "Don't send"
        dontSendAction.activationMode = UIUserNotificationActivationMode.Background
        dontSendAction.destructive = true
        dontSendAction.authenticationRequired = false

        let actionsArray = NSArray(objects: sendAction, dontSendAction)

        var reminderNotification = UIMutableUserNotificationCategory()
        reminderNotification.identifier = "reminderNotification"
        reminderNotification.setActions(actionsArray as [AnyObject], forContext: UIUserNotificationActionContext.Minimal)

        let categoriesForSettings = NSSet(objects: reminderNotification)

        let newNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: categoriesForSettings as Set<NSObject>)
        UIApplication.sharedApplication().registerUserNotificationSettings(newNotificationSettings)
    }

...我这样安排我的通知:

var localNotification = UILocalNotification()
    localNotification.fireDate = NSDate(timeIntervalSinceNow: 3)
    localNotification.timeZone = NSTimeZone.defaultTimeZone()
    localNotification.alertBody = "Hey, inform your contacts !"
    localNotification.alertAction = "Send Message"
    localNotification.category = "reminderNotification"
    localNotification.soundName = UILocalNotificationDefaultSoundName

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

当我尝试此代码时,通知中心会显示正确的通知,但没有显示弹出窗口... 我在网上搜索了资源,我不明白为什么这些示例和教程都显示弹出窗口,因为我编写了相同的代码......

感谢您的帮助,祝您有美好的一天!

编辑:我忘记了一些事情:必须检测事件的是通知中心,而不是区域设置通知。所以我想让通知中心的 addObserver 检查一个事件,是否可以在后台状态?

【问题讨论】:

    标签: ios swift notifications popup


    【解决方案1】:

    根据这一行:localNotification.fireDate = NSDate(timeIntervalSinceNow: 3) 您希望在 3 秒后触发您的通知。我假设该应用程序在发生时仍处于前台。如果是这种情况,您将不会弹出通知。

    如果链接到通知的应用在收到通知时打开,它必须在您的AppDelegate 中处理它以显示自定义弹出窗口。

    func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!) {
            // do your jobs here
    }
    

    【讨论】:

    • 感谢您的回答,我刚刚将“scheduleNotification”更改为“presentLocalNotificationNow”,这使得通知直接出现在通知中心。但事实上我希望出现一个弹出窗口,即使应用程序在后台运行(我启用了后台执行模式以继续运行应用程序并且我正在使用)
    • 使用您当前的代码,当应用程序按预期处于后台时,弹出窗口应显示。当应用程序打开时它不会显示(并且没有什么可以改变的)。
    • 好的,谢谢,但是当应用程序在后台时,通知不会显示。在设备上激活它有什么特别的事情要做吗?我在项目中激活了后台执行模式,并在Info.plist中添加了
    • 不,您甚至不必启用后台模式。但是既然你立即触发你的通知,你怎么能在后台测试它呢?放一个“timeIntervalSinceNow: 20”,调用你的触发函数的方法,把你的应用放到后台等待15秒看看。
    • 我编辑了这个问题,因为我忘记了一个大错误,请原谅我
    猜你喜欢
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 2014-08-19
    • 2022-08-22
    • 2011-09-30
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多