Can Poyrazoğlu
Can Poyrazoğlu

Reputation: 34830

Checking whether iOS app is launched from push properly

I am trying to detect if my app was launched from push, and there are tons of threads and answers. They are all wrong in my case, and lead me to writing incorrect behavior. They all tell to write roughly this:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    BOOL wasLaunchedFromPush = NO;
    if (application.applicationState == UIApplicationStateInactive) {
        wasLaunchedFromPush = YES;
    }
    ...
}

Or this:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    BOOL wasLaunchedFromPush = NO;
    if (application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground) {
        wasLaunchedFromPush = YES;
    }
    ...
}

When I try this, my app thinks that it was started from a notification when I'm on a phone call, the app is in the background, pretty much always when the app is running but not directly active. When my users return to the app, app acts like they've tapped the notification and opened it, whereas the user hasn't interacted with it at all. I don't know why people accepted that as a correct answer in many questions.

How can I simply check if my app is launched from a notification or not, also regardless of the previous state of the app (running in the background or just launched)?

Upvotes: 2

Views: 217

Answers (3)

Ewan Mellor
Ewan Mellor

Reputation: 6857

Checking against UIApplicationStateBackground is definitely wrong. If your push notifications are marked with content-available: 1 then your app is going to be called in that state so that you can perform your content download. This will happen without any user interaction.

The best I know of is using the check against UIApplicationStateInactive alone (i.e. your first code example).

As you say, this doesn't work if the notification arrives while your app has been interrupted by a phone call. That's an interesting problem. You could maybe keep track of call state using CTCallCenter but I've not done that and I don't know if you need VOIP permission to make it work.

Upvotes: 1

rmp
rmp

Reputation: 3523

If you only want to know when your app was launched from a remote notification, you should check for this in - application:didFinishLaunchingWithOptions: or - application:willFinishLaunchingWithOptions:. You can check for the presence of UIApplicationLaunchOptionsRemoteNotificationKey in the launchOptions dictionary.

didReceiveRemoteNotification is call when your app is running and a remote notification is received. It is called regardless of any user interaction this is because if the app is running, there will not be a notification (pop up) for the user to interact with.

Upvotes: 1

Adnan Aftab
Adnan Aftab

Reputation: 14477

There is no simple way which is going to tell you that its open from pushNotificaiton specially when app is in active or in background. You have to do some work around,

UIApplicationStateInactive The app is running in the foreground but is not receiving events. This might happen as a result of an interruption or because the app is transitioning to or from the background

This is best candidate for the phone call interruption, You can also add some other work around to check if app ever entered in background or not, if yes then after that if didReceiveRemoteNotification method get called its means its from push notification.

Upvotes: 1

Related Questions