Search code examples
iospush-notificationios10usernotifications

Background fetch execution time with iOS 10 UserNotifications without user interaction


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?


Solution

  • 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
          }