ios - 不显示本地通知横幅

标签 ios swift notifications

我正在制作一个需要向用户发送通知的应用程序。到目前为止,我已经在 App Delegate 中注册了通知:

let notifTypes: UIUserNotificationType = [.Alert, .Badge, .Sound]
    let notifSettings = UIUserNotificationSettings(forTypes: notifTypes, categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(notifSettings)

然后调用以下代码在 View Controller 中发送通知:

 let localNotification: UILocalNotification = UILocalNotification()
 localNotification.alertAction = "Testing notifications on iOS8"
 localNotification.alertBody = "Here is a notification"
 localNotification.fireDate = NSDate(timeIntervalSinceNow: 5)
 localNotification.soundName = UILocalNotificationDefaultSoundName
 UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

如果我下拉通知中心面板,会显示通知,但如果我不在应用程序中,则不会显示横幅,如果手机处于关机状态,锁定屏幕上不会显示通知。如果我不在应用程序之外,如何让通知显示在锁定屏幕上/作为横幅显示?

最佳答案

当通知触发时,您以用户角色看到的内容取决于您在首选项中的设置。您需要转到“设置”并将此应用配置为显示横幅并在锁定屏幕中显示通知。

我还建议,出于测试目的,您应该将通知安排在稍晚的时间间隔。你说的是 NSDate(timeIntervalSinceNow: 5) - 这不是让你自己离开应用程序的时间!通知可能会在您仍在应用程序中时触发——在这种情况下不会出现横幅。

关于ios - 不显示本地通知横幅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31487410/

相关文章:

ios - 导航 Controller 内的选项卡栏实现

iphone - core-plot,无法绘制两个图

ios - 同一父 View 中的两个容器 View 使用相同的数据

ios - 无法让 S3 上传在 Xcode 10.2/Swift 中工作

ios-charts 仅在数据下方绘制网格线

ios - 在UICollectionViewCell中使用Button编辑难点

ios - 如何设置 Apple 推送通知服务中的声音? (总是默认的?)

android - 与蓝牙断开连接后振动并不总是有效

android - 通过代码访问 Android 4.3 中新的通知设置

ios - 如何在 SwiftUI 中实现协议(protocol)和委托(delegate)