问题描述
在iOS 10之前的Push Notification API中,我可以轻松地运行由收到Push Notification触发的后台作业.刚收到-用户根本不需要与之交互.现在看来,为了使用该新框架执行相同的后台作业,用户必须以某种方式与通知进行交互.当客户端设备刚刚接收到某个推送但用户未与之交互时,这种新框架是否有办法仅运行后台作业?我可以在推送的用户信息字典中传递一种方法吗?
In the Push Notification API before iOS 10, I could easily run a background job triggered by a Push Notification being received. Just received -- the user didn't have to interact with it at all. Now it seems in order for my same background jobs to be carried out with this new framework the user must interact with the notification somehow. Is there a way with this new framework to just run a background job when a certain push is just received by the client device, but not interacted with by the user? Can I pass a method in the push's user info dictionary?
推荐答案
我发现了一个不能直接与 UserNotifications
框架一起使用的解决方案,但仍然允许您通过以下方式在iOS 10中执行后台作业推送通知.
I found a solution that doesn't work directly with the UserNotifications
framework but still allows you to perform background jobs in iOS 10 via push notifications.
我发现通过使用 UIApplicationDelegate
,您实际上可以通过推送通知执行后台作业,方法是使用以前的iOS版本中的相同功能:
I found that by using the UIApplicationDelegate
you can in fact perform background jobs via push notifications through the same function used in previous iOS versions:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler{
//execute the background job here
}
您要做的就是在发送前将 content-available
标志添加到推送中并将其设置为1.
All you have to do is add the content-available
flag to the push before sending it and set it equal to 1.
对于js一样:
data: {
alert: "Some alert",
type: someType,
"content-available": 1,
id: someId
}
这篇关于在没有用户交互的情况下,使用iOS 10 UserNotifications进行后台获取执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!