如何在ios 10中处理后台推送通知?

时间:2017-06-24 06:44:30

标签: ios objective-c push-notification ios10

我不在后台处理推送通知。

按照以下步骤在后台处理推送通知: -

  1. 在功能中 - >启用远程通知。
  2. 在功能中 - >背景模式 - >启用远程通知。
  3. 在didFinishLaunchingWithOptions中授予ios 10的所有权限。
  4. 使用推送通知UNUserNotificationCenter
  5. App在Foreground中,推送通知工作正常,方法调用正确:

    userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
    
  6. 但是我的问题是app在后台然后没有调用任何方法。所以任何人都有想法或解决方案在ios 10的后台处理推送通知然后请帮助我。

    感谢。

3 个答案:

答案 0 :(得分:6)

当app在前台时,会调用

willPresentNotification 。看看他们的文档

 - (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    // The method will be called on the delegate only if the application is in the foreground.
    // If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented.
    // The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list.
    // This decision should be based on whether the information in the notification is otherwise visible to the user.

}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void(^)())completionHandler {
    // The method will be called on the delegate when the user responded to the notification by opening the application,
    // dismissing the notification or choosing a UNNotificationAction.
    // The delegate must be set before the application returns from applicationDidFinishLaunching:.

}

尝试检查didReceiveNotificationResponse您将获得所需内容。

ALSO 如果需要获取任何数据或任何处理,请在后台模式中启用后台提取并使用以下方法

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{

    completionHandler(UIBackgroundFetchResultNewData);
}

根据申请状态处理APNS

   if(application.applicationState == UIApplicationStateInactive)
     {
        /* 
        # App is transitioning from background to foreground (user taps notification), do what you need when user taps here!
         */    
    }
    else if(application.applicationState == UIApplicationStateActive)
    {
        /*
         # App is currently active, can update badges count here
       */
    }
    else if(application.applicationState == UIApplicationStateBackground)
    {
        /* # App is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here */
    }

答案 1 :(得分:1)

这是我的解决方案:

{  
    "aps" : {  
        "content-available" : 1  
    },  
    "acme1" : "bar",  
    "acme2" : 42  
}  

重要的是将内容可用于1。

答案 2 :(得分:1)

当收到包含alert键的推送通知时,iOS将向用户显示该通知。如果用户与警报交互,则您的应用程序将启动到前台模式。

静默通知是包含content-available键的推送通知。这些通知不会显示给用户,并且不能包含alertsoundbadge键。当iOS发出静默通知时,您的应用程序将启动到后台模式。响应无提示通知,该应用程序可以在后台执行非常有限的工作。

无提示通知如下:

{
    "aps" : {
        "content-available" : 1
    },
    "com.yourcompany.something.something" : "something"
}

交付时,将调用应用程序委托方法application:didReceiveRemoteNotification:fetchCompletionHandler:。您对该方法的实现有30秒的时间来调用在fetchCompletionHandler参数中传递的代码块。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {  
    completionHandler(UIBackgroundFetchResultNoData);
    return;
}