ios - UILocalNotification 无法正常工作

标签 ios iphone xcode ipad swift

这是我的代码。我必须为 localNotification 添加什么吗?有什么建议。提前致谢。我阅读了很多本地通知教程。我不知道错过了什么。

var localNotifications : UILocalNotification!

    func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {

        localNotifications  = UILocalNotification()
        localNotifications.fireDate = NSDate(timeIntervalSinceNow: 20)
        localNotifications.timeZone = NSTimeZone.defaultTimeZone()
        localNotifications.alertBody = "Check it out!"
        localNotifications.soundName = UILocalNotificationDefaultSoundName
        localNotifications.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
        UIApplication.sharedApplication().scheduleLocalNotification(localNotifications)

 func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {

        println("Alvin Notification recieved")

        if application.applicationState == UIApplicationState.Active
        {
            // show alertView

            Utilities.sharedInstance.alertThis("Hello", message: "Hey")
        }
        else if application.applicationState == UIApplicationState.Background
        {
            UIApplication.sharedApplication().presentLocalNotificationNow(localNotifications)
        }
        else if application.applicationState == UIApplicationState.Inactive
        {
            UIApplication.sharedApplication().presentLocalNotificationNow(localNotifications)
        }

        application.applicationIconBadgeNumber = 0
    }

最佳答案

来自Local and Remote Notification Programming Guide :

In iOS 8 and later, apps that use either local or remote notifications must register the types of notifications they intend to deliver. The system then gives the user the ability to limit the types of notifications your app displays. The system does not badge icons, display alert messages, or play alert sounds if any of these notification types are not enabled for your app, even if they are specified in the notification payload.

如指南所示,Apple 展示的示例代码是:

    UIUserNotificationType types = UIUserNotificationTypeBadge |
             UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

UIUserNotificationSettings *mySettings =
            [UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

关于ios - UILocalNotification 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27376233/

相关文章:

xcode - 无法加载网络链接调节器首选项 Pane

ios - UITableViewSource 单元格高度始终为 44 点

ios - 是否有开源的 Flurry 替代品?

iphone - 将 iPhone xib 应用程序自动调整为 ipad

iphone - 正确使用 viewDidUnload

iPhone : Does a rapidly updating (60Hz) accelerometer drain battery?

ios - iPhone 模拟器屏幕截图尺寸不正确

ios - 在 iOS 中处理非常大的 JSON 对象

ios - 正确使用 Alamofire 的 URLRequestConvertible

Xcode 永远不会完成构建并耗尽计算机上的内存