Background fetch execution time with iOS 10 UserNotifications without user interaction

烈酒焚心 提交于 2020-01-15 05:55:07

问题


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?


回答1:


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.

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

}

All you have to do is add the content-available flag to the push before sending it and set it equal to 1.

Like so for js:

data: {
         alert: "Some alert",
         type: someType,
         "content-available": 1,
         id: someId
      }


来源:https://stackoverflow.com/questions/39584721/background-fetch-execution-time-with-ios-10-usernotifications-without-user-inter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!