在我们的应用程序中,用户将在开始工作时打卡。如果用户忘记签退,我们将在签到时间 24 小时后自动为其签退。应用程序可能不会长时间处于事件/后台状态。它可能会被终止。所以我们的想法是发布一个本地通知,通过该通知将在后台执行代码以让他下类。该通知必须是无声通知。但我们从研究中了解到,本地通知不能保持沉默。那么我们还有其他方法可以实现这一目标吗?或者我们实际上可以安排一个静默的本地通知吗?
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)
}
最佳答案
如果您希望通知保持静音并执行后台工作,则它必须是推送通知。如果没有一些用户交互,本地通知无法唤醒您的应用。
实现您想要的效果的最佳方法是使用核心位置区域监控在用户实际离开工作场所时唤醒您的应用程序。区域监控可以在后台静默唤醒您的应用程序,让您做一些工作。问题是它不能 100% 保证它会被触发,并且用户需要接受后台位置权限。
关于ios - 安排本地通知(无声)在后台 + swift ios 中执行自定义任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51515005/