Ios UILocalNotification不工作

Ios UILocalNotification不工作,ios,swift,uilocalnotification,Ios,Swift,Uilocalnotification,我正在尝试制作一个聊天应用程序,当你与另一个人聊天并收到另一个人的消息时,你会显示一个本地通知 到目前为止,我在我看来实现了这一点: else { // in case the user that sent the message // is not the same as the one you are currently talking to var partOfIt = msg.componentsSeparatedByString("\n")[1].compon

我正在尝试制作一个聊天应用程序,当你与另一个人聊天并收到另一个人的消息时,你会显示一个本地通知

到目前为止,我在我看来实现了这一点:

else {
    // in case the user that sent the message 
    // is not the same as the one you are currently talking to
    var partOfIt = msg.componentsSeparatedByString("\n")[1].componentsSeparatedByString(":")[6] as NSString
    var tuple = (partOfIt,fromuser)
    println("open up a new view")
    let notification: UILocalNotification = UILocalNotification()
    notification.timeZone = NSTimeZone.defaultTimeZone()

    let dateTime = NSDate(timeIntervalSinceNow: 2)
    notification.fireDate = dateTime
    notification.alertBody = "Woww it works!!"
    notification.alertAction = "Testing notifications on iOS8"
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
不幸的是,除了
println
之外,什么也没有发生。我允许在使用我的应用程序时显示通知

此外,我还想打开一个新的视角。当用户单击此通知时,我如何执行此操作


是我做错了什么吗?

只有当你的应用程序在后台时才会显示通知。甚至是本地的。 由于您在2秒钟内发送了通知,我猜您的应用程序仍处于打开状态


顺便说一句,您应该仍然能够在通知中心看到通知(即使您没有任何“弹出栏”)。

本地通知在
AppDelegate
中处理。如果你的应用程序正在运行,你必须实现
application:didReceiveLocalNotification:
,否则-你应该在
didFinishLaunchingWithOptions
中处理它(本地通知可在
UILocalNotificationKey
或类似的地方找到)

  • fireDate必须是将来的时间
  • 应用程序必须在后台运行,或已关闭
  • 还有一件事,别忘了显示查询是否允许推送,将以下代码添加到appDelegate:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
          UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
          [application registerUserNotificationSettings:settings];
        }
    }
    

  • 有没有办法启用弹出条?没有,你需要自己创建一个弹出条(所以它不是系统条),不久前我还没有遇到过同样的问题。有点无聊