3
votes

i am using parse push for my ios app (using xcode). I want to track the app and push-openings. The background state and inactive state are working, but foreground fails. While i'm receiving push in Foreground push open statistic stays on 0. This is what i have so far..

I hope somebody can help me :-)

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    if(application.applicationState == UIApplicationStateInactive) {

        NSLog(@"Inactive");
        [PFPush handlePush:userInfo];
        completionHandler(UIBackgroundFetchResultNewData);
        [PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];

    } else if (application.applicationState == UIApplicationStateBackground) {

        NSLog(@"Background");
        [PFPush handlePush:userInfo];
        completionHandler(UIBackgroundFetchResultNewData);
        [PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];

    } else {

        NSLog(@"Active");
        [PFPush handlePush:userInfo];
        completionHandler(UIBackgroundFetchResultNewData);
        [PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];

    }
}
1

1 Answers

1
votes

I want to track the app and push-openings. The background state and inactive state are working, but foreground fails

Parse doesn't actually track push notifications being received when the app is in the foreground. I guess the reason for that is that the purpose of a push notification is to make the user bring the app from the background to the foreground. If the app is in the foreground already, the push notification is redundant to that user and therefore to your analytics.

But if you are still interested in tracking users receiving push notifications when the app is in the foreground you can do two things:

A) Implement your app delegate's application:didReceiveRemoteNotification: function like this:

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
        PFPush.handlePush(userInfo)
    }

Compare this with Parse's recommended implementation, which calls trackAppOpenedWithRemoteNotificationPayload only if application.applicationState == .Inactive

So this means that Parse's Push Analytics will count all pushes received as a "Push Opens".

B) Track received push notifications when app is in the foreground as a separate analytics event:

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

        if application.applicationState == .Inactive  {
            // The application was just brought from the background to the foreground,
            // so we consider the app as having been "opened by a push notification."
            PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
        }
        else if application.applicationState == .Active {
            PFAnalytics.trackEvent("Received Push while app was in the foreground")
        }

        PFPush.handlePush(userInfo)
    }