Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send silent push notification to app, update location and send to server in background

I want to send a silent push notification to an application that is in background, then fetch the current user location and send it to a web service.

I implemented push notification methods and also those two:

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSDate *fetchStart = [NSDate date];

    [self sendLocationToServerWithCompletionHandler:^(UIBackgroundFetchResult result) {
        completionHandler(result);

        NSDate *fetchEnd = [NSDate date];
        NSTimeInterval timeElapsed = [fetchEnd timeIntervalSinceDate:fetchStart];
        NSLog(@"Background Fetch Duration: %f seconds", timeElapsed);

    }];
}

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

}

I've also created a method that will send the location to the server:

- (void)sendLocationToServerWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    NSDictionary *params = @{
                             @"UserId"      : self.userId,
                             @"Latitude"    : self.latitude,
                             @"Longitude"   : self.longitude
                             }

    ServerManager *manager = [ServerManager sharedManager];
    [manager sendLocationToServerWithCompletion:^(BOOL success) {

        if (success)
        {
            completionHandler(UIBackgroundFetchResultNewData);
        }
        else
        {
            completionHandler(UIBackgroundFetchResultFailed);
        }

    }];
}

I just can't understand how they all work together, will Apple approve that, is it even possible and where does the location background fetch goes into.

Thanks in advance.

like image 592
ytpm Avatar asked Sep 17 '15 17:09

ytpm


People also ask

How do I send silent push notifications?

Control Panel. To send a Silent Push Notification to your mobile app, check the checkbox Silent Push in the iOS and Android push settings of the Send Push form.

Will iOS awake my app when I receive silent push notification?

Silent remote notifications can wake your app from a “Suspended” or “Not Running” state to update content or run certain tasks without notifying your users.

What is background push notification?

Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device's system tray. A user tap on a notification opens the app launcher by default. Messages with both notification and data payload, when received in the background.


1 Answers

Here's a brief sketch of what you can do to give you an idea. Its assuming there is a model class implemented as a singleton and there's some pseudo code.

// App delegate
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    completionHandler(UIBackgroundFetchResultNewData);
    [[YourModel singleton] pushNotificationReceived: userInfo];
}

// Model
- (void)  pushNotificationReceived:(NSDictionary *) userInfo
{
    [self registerBackgroundTaskHandler];
    get the location here, or start getting the location
    [self sendLocationToServerWithCompletionHandler: your completion handler];
}


- (void) registerBackgroundTaskHandler
{
    __block UIApplication *app = [UIApplication sharedApplication];
    self.backgroundTaskId = [app beginBackgroundTaskWithExpirationHandler:^{
        DDLogInfo(@"BACKGROUND  Background task expiration handler called");
        [app endBackgroundTask:self.backgroundTaskId];
        self.backgroundTaskId = 0;
    }];
}


- (void) endBackgroundTask
{
    if (self.backgroundTaskId)
    {
        UIApplication *app = [UIApplication sharedApplication];
        [app endBackgroundTask:self.backgroundTaskId];
        self.backgroundTaskId = 0;
    }
}

You'll need to get the location before you can send it. If you are just getting one location and you're using iOS9 you can use CLLocationManager:requestLocation: and you could fit this in relatively easily into where I've said "get the location here".

If you're not using iOS 9 (requestLocation is new with iOS 9) its a bit more complex. How to use the location manager is a topic in itself and too much code to post here. You need to read and study all about using the location manger before you can incorporate it. If you need a stream of location updates it gets more complex and where it says "or start getting the location" is a lot more involved then is implied in the pseudo code.

My recommendation, start with iOS9 and getting one instance of the location, then when thats working, add more functionality or iOS8 support if you need it.

like image 179
Gruntcakes Avatar answered Oct 22 '22 10:10

Gruntcakes