如何在 ios 10 中处理后台推送通知?
How to handle push notification in background in ios 10?
我不会在后台处理推送通知。
按照以下步骤在后台处理推送通知:-
- 在功能 -> 启用远程通知中。
- 在功能 -> 后台模式 -> 启用远程通知。
- 在 didFinishLaunchingWithOptions 中给予 ios 10.
的所有权限
- 用于推送通知
UNUserNotificationCenter
。
App 在前台,然后推送通知工作正常,下面的方法调用:
userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
但我的问题是应用程序在后台然后不调用任何 method.so 任何人有关于在后台处理推送通知的想法或解决方案 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:.
}
尝试签到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 */
}
这是我的解决方案:
{
"aps" : {
"content-available" : 1
},
"acme1" : "bar",
"acme2" : 42
}
重要的是把content-available设为1。
收到包含 alert
键的推送通知后,iOS 将向用户显示通知。如果用户与警报交互,您的应用程序将启动到前台模式。
静默通知是包含 content-available
键的推送通知。这些通知 不会 呈现给用户,并且不能包含 alert
、sound
或 badge
键。当 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;
}
我不会在后台处理推送通知。
按照以下步骤在后台处理推送通知:-
- 在功能 -> 启用远程通知中。
- 在功能 -> 后台模式 -> 启用远程通知。
- 在 didFinishLaunchingWithOptions 中给予 ios 10. 的所有权限
- 用于推送通知
UNUserNotificationCenter
。 App 在前台,然后推送通知工作正常,下面的方法调用:
userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
但我的问题是应用程序在后台然后不调用任何 method.so 任何人有关于在后台处理推送通知的想法或解决方案 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:.
}
尝试签到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 */
}
这是我的解决方案:
{
"aps" : {
"content-available" : 1
},
"acme1" : "bar",
"acme2" : 42
}
重要的是把content-available设为1。
收到包含 alert
键的推送通知后,iOS 将向用户显示通知。如果用户与警报交互,您的应用程序将启动到前台模式。
静默通知是包含 content-available
键的推送通知。这些通知 不会 呈现给用户,并且不能包含 alert
、sound
或 badge
键。当 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;
}