【问题标题】:UILocalNotification not working properlyUILocalNotification 无法正常工作
【发布时间】:2015-02-07 04:26:36
【问题描述】:

这是我的代码。有什么我必须为 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
    }

【问题讨论】:

  • 通知未显示。?
  • 是的。它适用于 iOS 8。
  • 打印Alvin Notification recieved??
  • 你的应用在后台吗

标签: ios iphone xcode ipad swift


【解决方案1】:

来自Local and Remote Notification Programming Guide:

在 iOS 8 及更高版本中,使用本地或远程通知的应用程序必须注册它们打算发送的通知类型。然后,系统使用户能够限制您的应用显示的通知类型。如果您的应用未启用任何这些通知类型,即使在通知负载中指定了这些通知类型,系统也不会标记图标、显示警报消息或播放警报声音。

如指南所示,苹果展示的示例代码为:

    UIUserNotificationType types = UIUserNotificationTypeBadge |
             UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

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

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

【讨论】:

    【解决方案2】:

    Swift 的方式是这样的:

    UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil))
    

    更新:Xcode 7 • Swift 2.0

    UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert , .Sound, .Badge], categories: nil))
    

    【讨论】:

      猜你喜欢
      • 2016-12-01
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      • 2012-07-11
      • 2018-04-08
      • 2017-04-20
      • 2018-10-02
      • 2016-09-04
      相关资源
      最近更新 更多