Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在用户离开应用程序IOS时向其发送本地通知_Ios_Xcode_Notifications_Notificationcenter - Fatal编程技术网

如何在用户离开应用程序IOS时向其发送本地通知

如何在用户离开应用程序IOS时向其发送本地通知,ios,xcode,notifications,notificationcenter,Ios,Xcode,Notifications,Notificationcenter,我正在开发一个计时器应用程序,如果用户离开应用程序,该应用程序将终止。我希望在用户离开应用程序时向其发送通知。此通知警告用户,如果用户不立即返回应用程序,计时器将终止 我看过UnnotificationTrigger,但没有看过我尝试过的,如果用户被警告不止一次,我会重复通知 如何检测用户是否在应用程序之外,然后发送通知警告他们返回应用程序?? 先谢谢你 代码: 并且还将相同的代码放在appdelegate的willforeground方法中,以了解用户是否即将离开应用程序 func ap

我正在开发一个计时器应用程序,如果用户离开应用程序,该应用程序将终止。我希望在用户离开应用程序时向其发送通知。此通知警告用户,如果用户不立即返回应用程序,计时器将终止 我看过UnnotificationTrigger,但没有看过我尝试过的,如果用户被警告不止一次,我会重复通知

如何检测用户是否在应用程序之外,然后发送通知警告他们返回应用程序?? 先谢谢你

代码:

并且还将相同的代码放在appdelegate的willforeground方法中,以了解用户是否即将离开应用程序

   func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

将此方法放在appdelegate中,如果定义了alredy func application willterminate(u-application:ui-application),则只放代码:)

您可以使用applicationIdentinterbackground(u-application:ui-application)来了解用户何时进入后台。是否收到本地通知?@Anuraj我仍然有问题,当我最初运行应用程序时,本地通知将出现一次,但当我返回应用程序时,计时器将停止,如果我在同一运行会话中退出应用程序,通知将不会出现。@Christopher Babafijimadeogun有类似willPresent和all的用户通知的implemet委托方法???@Anuraj你的意思是,是否导入了“导入用户通知”方法?如果是,那么是。@Anuraj我已将代码放在3个应用程序代理中。。。DidEnterBackground、applicationwillenterforeground和application将终止。这些通知现在似乎起作用了。谢谢你知道我如何设置一个时间阈值,让用户在apptimer停止之前有一定的秒数返回应用程序吗?@Christopher BabafijimiAdeogun是的,你可以在didEnterBackground上设置后台计时器几分钟,如果用户在该时间段内返回,你可以在中设置该计时器didEnterForegroundMethod。如果这个答案对你有帮助,那么请把我的帖子标记为正确
func applicationWillTerminate(_ application: UIApplication) {

    let content = UNMutableNotificationContent()

    //adding title, subtitle, body and badge
    content.title = "Hey if you will not come back"
    content.subtitle = "your timer will be killed"
    content.body = ""
    content.badge = 1

    //getting the notification trigger
    //it will be called after 5 seconds
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

    //getting the notification request
    let request = UNNotificationRequest(identifier: "SimplifiedIOSNotification", content: content, trigger: trigger)

    //adding the notification to notification center
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
   func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }