最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift的iPhone真机的本地通知延迟很久才提示

iphone crifan 2428浏览 0评论

iPhone6 真机 iOS 9.2.1中,本地通知:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
。。。
        //enable notification
        let notifSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound],
            categories: nil)
        application.registerUserNotificationSettings(notifSettings)
        application.beginBackgroundTaskWithName("showNotification", expirationHandler: nil)
        //reset badge number
        application.applicationIconBadgeNumber = 0
       
        return true
    }
    func sendNotification(newMessage:Message){
        var notifTitle = ""
        var notifBody = ""
        (notifTitle, notifBody) = genMesageTitleBody(newMessage)
        print("notifTitle=\(notifTitle), notifBody=\(notifBody)")
        UIApplication.sharedApplication().cancelAllLocalNotifications()
       
        let localNotif = UILocalNotification()
        localNotif.fireDate = NSDate(timeIntervalSinceNow: 0.1)
        localNotif.timeZone = NSTimeZone.defaultTimeZone()
        if #available(iOS 8.2, *) {
            localNotif.alertTitle = notifTitle
        } else {
            // Fallback on earlier versions
        }
        localNotif.alertBody = notifBody
        localNotif.soundName = UILocalNotificationDefaultSoundName
        localNotif.alertLaunchImage = ""
        localNotif.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
        UIApplication.sharedApplication().scheduleLocalNotification(localNotif)
    }

要等很久才提示

或者根本就不工作,不提示

但是在iOS 模拟器中,却工作的非常稳定:

只要有scheduleLocalNotification就会立刻出发对应的本地通知

swift local notification sometime not work

ios local notification sometime not work

iphone – Local Notification is not working well in ios – Stack Overflow

objective c – iOS (iPhone) Development – Local notifications randomly fire while app is not running – Stack Overflow

ios – Local notifications are not working on device but working on simulator – Stack Overflow

ios – Send Local Notifications while App is running in the background Swift 2.0 – Stack Overflow

去加上试试:

【总结】

        //enable notification
        let notifSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound],
            categories: nil)
        application.registerUserNotificationSettings(notifSettings)
        application.beginBackgroundTaskWithName("showNotification", expirationHandler: nil)

果然可以了:

iPhone6真机 iOS9.2.1中,发了消息后:

马上(等待时间不到1秒)就可以收到本地通知了:

然后锁屏后,也可以显示本地通知了:

转载请注明:在路上 » [已解决]swift的iPhone真机的本地通知延迟很久才提示

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.187 seconds, using 22.13MB memory