- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
所以我们的应用程序使用 UILocalNotifications,它被安排在每天早上 9 点。此消息的内容取决于 API 调用,我想在安排通知前 5 分钟进行调用。
因此,例如,通知可能被设置为提醒用户执行任务 x,但用户有可能已经完成了任务 x(通过在线门户而不是应用程序),在这种情况下我们想告诉他们改为执行任务 Y。由于各种原因,我们尚未设置推送通知服务器,但很快就会设置,因此这是用于测试目的的临时解决方案。
所以我的问题是:我如何安排一个 API 调用来根据响应替换即时通知消息,即使应用程序在后台,甚至关闭?
谢谢!
最佳答案
您必须使用静默推送通知。在有效载荷中,您可以从服务器获取各种信息。然后在上午 9:00 安排 UILocalNotification
。
当您使用静默推送通知时,您的应用程序将在后台/终止状态(注意 - 不会出现在前台)调用直到您的 UILocalNotification
声音文件播放(最多 30 秒),在这 30 秒内你可以做 API 相关的工作。
当 UILocalNotification
显示在通知中心时,例如有 2 个通知按钮“已接受任务”和“稍后”。因此,当您点击按钮时,即使您的应用程序也会在后台/终止状态下调用,您也可以打开应用程序并重定向到特定的 View Controller 。对于这两种情况,您都可以进行 API 相关工作。
如果您将 UILocalNotification
对象保存在 NSUserDefault
中,那么您还可以在 didFinishLaunchingWithOption
上检索它,以防您的设备重新启动并且 UILocalNotification
和进一步的 API 工作非常关键。
让我知道您需要静默推送通知、UILocalNotification
、didFinishLaunchingWithOption
或任何帮助。
推荐备注
https://www.raywenderlich.com/123862/push-notifications-tutorial
https://www.sinch.com/tutorials/ios8-apps-and-pushkit/
https://developer.apple.com/reference/pushkit
来源 https://github.com/hasyapanchasara/PushKit_SilentPushNotification
推送服务实现代码
导入 UIKit导入 PushKit
AppDelegate 类:UIResponder、UIApplicationDelegate、PKPushRegistryDelegate{
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
application.registerForRemoteNotificationTypes(types)
self. PushKitRegistration()
return true
}
//MARK: - PushKitRegistration
func PushKitRegistration()
{
let mainQueue = dispatch_get_main_queue()
// Create a push registry object
if #available(iOS 8.0, *) {
let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)
// Set the registry's delegate to self
voipRegistry.delegate = self
// Set the push type to VoIP
voipRegistry.desiredPushTypes = [PKPushTypeVoIP]
} else {
// Fallback on earlier versions
}
}
@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
// Register VoIP push token (a property of PKPushCredentials) with server
let hexString : String = UnsafeBufferPointer<UInt8>(start: UnsafePointer(credentials.token.bytes),
count: credentials.token.length).map { String(format: "%02x", $0) }.joinWithSeparator("")
print(hexString)
}
@available(iOS 8.0, *)
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
// Process the received push
// From here you can schedule UILocalNotification
}
}
关于ios - 即使应用程序在后台运行或根本不运行,我如何安排一个将执行的函数(以执行 API 调用)。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39702812/
我遇到了一个似乎很独特的问题。我的 NSUbiquitousKeyValueStore 在模拟器中的启动之间根本不起作用。也就是说,我什至不是在谈论 iCloud 同步或类似的东西,我无法让它通过下面
首先,我使用的是 WiX 版本 3.5.2519.0,但我也在最新的 3.6 版本上测试了它,结果相同。 我很难确定 PatchFamily 究竟能过滤掉 torch 生成的差异的某些部分。按照手册中
我可以获取要呈现的“帮助主题”标题,但无法获取我定义的任何FIXTURES。 {{#each model}} 中的任何内容都不会渲染。这是我第一次使用 Ember,所以任何东西(字面意义上的任何东
我一直在尝试设置custom ajaxTransports for jQuery在我们的产品的某些场景下缩短某些工作流程。然而,我在让这些传输受到尊重方面取得了零成功(而我有很多工作 custom a
为什么纯无类型 lambda 演算经常被描述为无法使用? 有了合适的函数库,它会不会与任何其他函数式语言大致相同? 最佳答案 速度不是大问题。例如,您可以决定使用教堂数字但优化实现,以便像往常一样表示
我是一名优秀的程序员,十分优秀!