且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-24 19:25:04

willPresentNotification 应用程序处于前台时调用strong>。查看他们的文档

 - (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 你会得到你需要的东西。

Try to check in didReceiveNotificationResponse you will get what you need.

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

ALSO If need to fetch any data or any processing, Enable background fetch in background modes and use below method

-(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 */
    }