I am working on an app in which I have implemented apple push notification. When my app is in background state then I am able to receive push notifications but when my app is in active state then I am not able to get any push notifications, can anypne help on this?
-
could you please share some code, so that it is easy for anyone to help – Developer Jul 12 '17 at 10:16
-
see this https://stackoverflow.com/questions/39382852/didreceiveremotenotification-not-called-ios-10/39383027#39383027 – Anbu.Karthik Jul 12 '17 at 10:23
-
my push notifications service is not working for ios device but its working for android.. can any tell me what will be the issue? and were i went wrong? – Charishma Vasamsetti Jul 26 '17 at 12:05
3 Answers
Try to check inside didReceiveRemoteNotification
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
delegate method to receive remote notification in all state.
If you want to show the notification alert in active state. use HDNotificationView
to show notification alert.
like this:
if(application.applicationState == UIApplicationStateActive) {
[HDNotificationView showNotificationViewWithImage:[UIImage imageNamed:@"Icon-40.png"]
title:title
message:message
isAutoHide:YES
onTouch:^{
/// On touch handle. You can hide notification view or do something
[HDNotificationView hideNotificationViewOnComplete:nil];
}];
}

- 642
- 1
- 11
- 25
Add an UIAlertView
inside didReceiveRemoteNotification
and check if you are receiving notification in active state
. Check below code
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (application.applicationState == UIApplicationStateActive) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Received a Notification" message:[NSString stringWithFormat:@"My App received notification while it is running:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}

- 285
- 1
- 13
When Push Notification received to your app Application can be one the below 5 states Reference:: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html
1)Not running
2)Inactive
3)Background
4)Suspended
5)Active
If application state is 1,2,3,4 then iOS will handle and will display banner In case of 5(Active state) our app needs to handle this case In this case below method will be called
Reference:: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application?language=objc
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
here you need to check state of app using application.state == Active then you can do what ever you want ideal case here we need to display alert .

- 6,303
- 27
- 42