2
votes

I have a didReceiveRemoteNotification handler in my App Delegate.

Even if I return a and empty code block, my app is getting a push notification and displaying it to the user when the app is running.

I want the push notification when the app is in the background, but NOT when it is running in the foreground.

I have read a dozen SO posts and the documentation on iOS8 notifications, and the the WWDC2014 video on notifications and I still can't figure out how to prevent the notification modal alert when the app is in the foreground.

If you know any way of doing this please advise me.

4
Can you add your didReceiveRemoteNotification code here??DilumN

4 Answers

3
votes

You may check application State in didReceiveRemoteNotification to achieve your functionality. If application state is active then do not execute your code.

For more reference, check below code.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{

    if(application.applicationState == UIApplicationStateActive) {
         // Do nothing
    }else{
         // Do your code here
    }
}
1
votes

My solution was to change my push to be silent, and send a local notification.

Make sure you set your 'content-available' to 1 in the payload and remove the 'alert' key.

1
votes

I'm faced with same issue and in my case the problem was with library "OneSignal". It seems like this library register their own callbacks for push notifications. Even if you have empty "didReceiveRemoteNotification" method in your code, OneSignal execute their own function after receiving a push and display alert window to user when app is open. I found solutions for this problem here.

0
votes

Just add this line to didFinishLaunchingWithOptions in the app Delegate

OneSignal.inFocusDisplayType = OSNotificationDisplayType.none