gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:29:51 26 4
gpt4 key购买 nike

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

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

  1. 在功能中 -> 启用远程通知。
  2. 在功能 -> 后台模式 -> 启用远程通知。
  3. 在 didFinishLaunchingWithOptions 中授予 ios 10 的所有权限。
  4. 对于使用 UNUserNotificationCenter 的推送通知。
  5. 应用程序在前台然后推送通知工作正常,下面的方法调用:

    userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler

但我的问题是应用程序在后台然后不调用任何方法。所以任何人有想法或解决方案在 ios 10 后台处理推送通知然后请帮助我。

谢谢。

最佳答案

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:.

}

尝试 checkin didReceiveNotificationResponse 您将获得所需的内容。

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

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

关于ios - 如何在 ios 10 中处理后台推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44733634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com