首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安排本地通知(静默)在后台执行自定义任务+ swift ios

安排本地通知(静默)在后台执行自定义任务+ swift ios
EN

Stack Overflow用户
提问于 2018-07-25 17:05:48
回答 1查看 1.8K关注 0票数 0

在我们的应用程序中,用户将在开始工作时打卡。如果用户忘记打卡退出,我们将不得不在24小时后自动为他打卡退出。应用程序可能不会处于活动/后台状态这么长时间。它可能会被终止。所以我们的想法是发布一个本地通知,通过它在后台执行代码来让他退出。此通知必须是静默通知。但我们从研究中得到的理解是,本地通知不能保持沉默。那么,有没有其他方法可以实现这一点呢?或者我们实际上可以安排一个静默的本地通知吗?

代码语言:javascript
复制
class func generateLocalNotificationWith(timeInterval : TimeInterval, title : String, message : String, mobileTimeClockId: Int)
{    
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    let content = UNMutableNotificationContent()

    //adding title, subtitle, body and badge
    content.title = title
    content.body = message

    let userInfoDictionary = ["badge":"0","content-available": "1"]

    let dict = ["aps": userInfoDictionary, "MobileTimeClockId": mobileTimeClockId] as [String : Any]

    content.userInfo = dict

    //getting the notification trigger
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, 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)
}
EN

回答 1

Stack Overflow用户

发布于 2018-07-25 17:20:02

如果您希望通知保持静默并执行后台工作,则它必须是推送通知。本地通知在没有用户交互的情况下无法唤醒您的应用程序。

做你想做的事情的最好方法是在用户离开工作场所时使用核心位置区域监控来唤醒你的应用。地域监控可以在后台静默唤醒你的应用,让你做一些工作。问题是,它不是百分之百确定它将被触发,用户将需要接受后台位置权限。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51515005

复制
相关文章

相似问题

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