UILocalNotifcation没有在swift 2.0中触发

时间:2016-09-20 18:37:59

标签: ios swift2 uilocalnotification

我正在使用推送通知,我已完成设置推送通知的所有步骤。

我能够在后台申请时收到通知,但是当前景中的应用程序登陆didReceiveRemoteNotification但未触发时。

这里我的代码在AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Alert, .Sound], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        if application.applicationState == UIApplicationState.Active {
            let localNotification = UILocalNotification()
            let date = NSDate(timeIntervalSinceNow: 10)
            localNotification.fireDate = date
            let timeZone = NSTimeZone.localTimeZone()
            localNotification.timeZone = timeZone
            localNotification.alertBody = "Sample Notification Body"
            localNotification.userInfo = userInfo
            print(localNotification)
            localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
            UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
        }
    }

我在做什么错误?

提前致谢

2 个答案:

答案 0 :(得分:1)

我怀疑是,当您使用本地通知启动应用时,您正尝试显示远程通知。

嗯,无论是本地通知还是远程,您都无法在应用处于活动状态时看到任何UI警报,而是相应地调用本地或远程通知的委托方法。

然而,iOS 10增加了在应用内查看它的功能。以下是讨论:https://stackoverflow.com/a/37844312/593709

要在iOS-10之前显示任何提醒,当您的应用处于有效状态时,您需要使用UIAlertController或其他一些实施,例如MPNotificationView

答案 1 :(得分:0)

当应用程序处于Foreground并且正在运行时,它不会显示通知横幅。仅当应用程序处于后台(非活动)或根本不运行时,才会显示横幅警报。委托方法

application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)

呼叫远程(推送)通知

application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)

需要本地通知

您可以使用任何第三方库bryx-inc/BRYXBanner来显示通知提醒消息的横幅。委托方法didReceiveRemoteNotification可用于更新徽章图标并在横幅视图中显示消息。