I am not handle push notification in background.
For Handle push notification in background following below steps :-
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.
Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.
Go to Settings and tap Notifications. Select an app under Notification Style. Under Alerts, choose the alert style that you want. If you turn on Allow Notifications, choose when you want the notifications delivered—immediately or in the scheduled notification summary.
A background push notification is a silent, remote notification that helps keep your app's data up to date. Unlike regular push notifications, background push notifications do not show app icon badges, trigger a sound, or display a message when received by your app.
Notifications could be created at any moment (on Foreground, Background or even when the application is terminated/killed).
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 */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With