Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS push notification received when app is running in foreground

From my understanding, when app is running or in the foreground and a push notification is received, the app should NOT show any alert but the app delegate will call the didReceiveRemoteNotification delegate method and I should handle the push notification in that callback.

The push notification should ONLY display alerts/banners when the app is in the background.

However, our app gets push notification alert with an OK button when the app is running or in the foreground sometime, not all of the time. I'm wondering if it is something new in iOS 7 (I have never heard of this) or is it because I'm using UrbanAirship for push notification for our iOS app using alias of the user. The app will display the push alert when running and run the callback in didReceiveRemoteNotification.

Scratching my head over this. Does anyone know why?

like image 845
imObjCSwifting Avatar asked Dec 25 '22 05:12

imObjCSwifting


2 Answers

When the App is in foreground, it should not display anything.

If you see alertView, it means you provided code for it.

Something along the following lines:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    UIApplicationState state = [application applicationState];

    if (state == UIApplicationStateActive) {
        //app is in foreground
        //the push is in your control
    } else {
        //app is in background:
        //iOS is responsible for displaying push alerts, banner etc..
    }
}

If you have implemented pushNotificationDelegate

[UAPush shared].pushNotificationDelegate = self;

then override, and leave it blank

- (void)displayNotificationAlert:(NSString *)alertMessage
{
  //do nothing
}
like image 188
meda Avatar answered Dec 28 '22 10:12

meda


You are most likely seeing the additional push notification because of the way Urban Airship is configured in your code. From Urban Airship's docs:

The sample UI includes an implementation of UAPushNotificationDelegate that handles alerts, sounds, and badges. However, if you wish to customize this behavior, you can provide your own implementation:

[UAPush shared].pushNotificationDelegate = customPushDelegate;

There is more information on the proper way to handle push notifications with Urban Airship in their support articles. Since you are using UA, I would recommend that you use their delegates etc. to handle incoming push notifications while in the foreground rather than implementing your own code in the didReceiveRemoteNotification app delegate method.

Hopefully that helps...if not, please post your code so that we can decipher what is going on. This would be very odd behavior indeed!

like image 34
BFar Avatar answered Dec 28 '22 08:12

BFar