Charishma Vasamsetti
Charishma Vasamsetti

Reputation: 95

How to get push notification when app is in active state in ios objective c?

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?

Upvotes: 1

Views: 1733

Answers (3)

Narayana Rao Routhu
Narayana Rao Routhu

Reputation: 6323

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 .

Upvotes: 0

Vikash Kumar
Vikash Kumar

Reputation: 636

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];
                                                          }];
}

Upvotes: 1

Vignesh Davins
Vignesh Davins

Reputation: 285

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];          
        }    
    }

Upvotes: 0

Related Questions