问题
I am not handle push notification in background.
For Handle push notification in background following below steps :-
- In Capabilities -> Enable Remote notification.
- In Capabilities -> Background Mode -> Enable Remote notifications.
- In didFinishLaunchingWithOptions give all permission for ios 10.
- For push notification used
UNUserNotificationCenter
. App In Foreground then push notification is working fine and below method call :
userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
But my problem is app in background then not call any method.so any one have idea or solution for handle push notification in background for ios 10 then please help me.
Thanks.
回答1:
willPresentNotification is called when app is in foreground. Have a look to their documentation
- (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:.
}
Try to check in didReceiveNotificationResponse
you will get what you need.
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);
}
Handling APNS on the basis of application states
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 */
}
回答2:
This was the solution for me:
{
"aps" : {
"content-available" : 1
},
"acme1" : "bar",
"acme2" : 42
}
What is important is to put the content-available to 1.
回答3:
When a push notification containing the alert
key is received iOS will present the notification to the user. If the user interacts with the alert your application will be launched into the foreground mode.
Silent notifications are push notifications that contain the content-available
key. These notifications are not presented to the user and can't contain the alert
, sound
, or badge
keys. When iOS delivers a silent notification your application is launched into the background mode. The application can perform a very limited amount of work in the background in response to the silent notification.
A silent notification looks like this:
{
"aps" : {
"content-available" : 1
},
"com.yourcompany.something.something" : "something"
}
When it is delivered the application delegate method application:didReceiveRemoteNotification:fetchCompletionHandler:
is called. Your implementation of that method has 30 seconds to call the block passed in the fetchCompletionHandler
parameter.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
completionHandler(UIBackgroundFetchResultNoData);
return;
}
来源:https://stackoverflow.com/questions/44733634/how-to-handle-push-notification-in-background-in-ios-10