UNUserNotification正在ios中立即发送通知

时间:2016-12-21 06:56:10

标签: ios swift notifications local

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

这是我的代码,

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)

}

1 个答案:

答案 0 :(得分:0)

尝试以这种方式使用它...而不是4你可以使用自己的时间......稍后在你的功能中你可以使用计时器概念每60秒发送一次通知。

selectedIndexPath