首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UNUserNotification在ios中即时发送通知

UNUserNotification在ios中即时发送通知
EN

Stack Overflow用户
提问于 2016-12-21 14:56:10
回答 1查看 724关注 0票数 0

我希望在应用程序进入后台后,每隔60秒发送一次通知。但当你进入后台时,通知是立即发送的,之后每60秒发送一次通知。我不想立即发送通知,我该怎么做?

这是我的代码,

代码语言:javascript
复制
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
        // Enable or disable features based on authorization.
    }
    return true
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    let content = UNMutableNotificationContent()
    content.title = NSString.localizedUserNotificationString(forKey: "Hello,", arguments: nil)
    content.body = NSString.localizedUserNotificationString(forKey: "Some", arguments: nil)
    content.sound = UNNotificationSound.default()
    content.badge = NSNumber(value: UIApplication.shared.applicationIconBadgeNumber + 1)
    content.categoryIdentifier = "com.mahmut.localNotification"

    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60.0, repeats: true)
    let request = UNNotificationRequest.init(identifier: "60Sec", content: content, trigger: trigger)

    let center = UNUserNotificationCenter.current()
    center.add(request)

}
EN

回答 1

Stack Overflow用户

发布于 2016-12-21 17:38:56

试着这样使用它...你可以利用你自己的时间而不是4小时。稍后,在您的函数中,您可以使用计时器概念每隔60秒发送一次通知。

代码语言:javascript
复制
    let triggerTime = (Int64(NSEC_PER_SEC) * 4)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, triggerTime), dispatch_get_main_queue(), { () -> Void in
        self.callYourFunction() 
    })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41256844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档