ios - UILocalNotification 没有做任何事情

标签 ios swift notifications uilocalnotification

这似乎是一个愚蠢的问题,但这是我第一次使用 UILocalNotification,我无法让它工作以进行快速测试。它只是什么都不做。

<强>1。我在 AppDelegate 中创建了 2 个变量

let today = NSDate()
let notification: UILocalNotification = UILocalNotification()

<强>2。然后在 applicationDidEnterBackground 函数中,我有以下内容

    notification.fireDate = today.dateByAddingTimeInterval(10)
    notification.alertTitle = "My App Test"
    notification.alertBody = "Testing Notification \n :)"
    UIApplication.sharedApplication().presentLocalNotificationNow(notification)
    UIApplication.sharedApplication().scheduleLocalNotification(notification)

<强>3。还将此添加到 applicationDidBecomeActive 函数中

UIApplication.sharedApplication().cancelAllLocalNotifications()

最佳答案

再次阅读文档后,我意识到我错过了关键的第一步,即首先注册我的应用程序以获取用户通知。 Apple 文档是用 OBJ-C 编写的,但我能够弄明白以便将其转换为 swift。这就是我所做的:

<强>1。我将它添加到我的 AppDelegate didFinishLaunchingWithOptions 函数中,它现在可以工作了

var types: UIUserNotificationType = UIUserNotificationType()
types.insert(UIUserNotificationType.Alert)
types.insert(UIUserNotificationType.Badge)

let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)

UIApplication.sharedApplication().registerUserNotificationSettings(settings)

关于ios - UILocalNotification 没有做任何事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32868884/

相关文章:

ios - 动画期间 subview 堆叠

ios - UIView 可以在初始化后加载另一个 Nib 吗?

ios - 使用 AssetsLibrary 框架 iPhone 访问库中的视频?

ios - 如何根据用户选择更新应用程序的整个 UI?

ios - 如何在 iOS 中为 UISearchBar 背景 View 应用圆角半径?

Android、Urban AirShip - 更改消息

iphone - 我应该在哪里删除通知观察者?

ios - 在带有 Objective-C 的 Xcode 8 中使用 UIAlertController

ios - 使用 JSON swift4 中的 key 解码结构

java - 从另一个类调用时,函数会使应用程序崩溃,但从 MainActivity.java 调用时不会崩溃