1

I want to update user location to server when silent notification is received. I had enabled push notification and background mode as remote notification and location. But my

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler

is not called when app is in killed or not running state. I am testing this by sending location data to pubnub. Main problem is that when i had started coding this part it was working perfectly but after some time it has stopped working and after that till now i am struggling with this. Any help will be appreciated.

Here is my code :

// In app delegate

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

    handler(UIBackgroundFetchResultNewData);
    [[RemoteNotification sharedInstance] pushNotificationReceived:userInfo];

}

//RemoteNotification Modal

- (void)  pushNotificationReceived:(NSDictionary *) userInfo
  {
   [self registerBackgroundTaskHandler];
   //get the location here, or start getting the location
   locationManager = [[CLLocationManager alloc] init];
   locationManager.desiredAccuracy= kCLLocationAccuracyBest;
   locationManager.delegate = self;
   [locationManager startUpdatingLocation];
 }


- (void) registerBackgroundTaskHandler
  {
    __block UIApplication *app = [UIApplication sharedApplication];

    self.backgroundTaskId = [app     beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:self.backgroundTaskId];
    self.backgroundTaskId = 0;
}];
}


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

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{

   [locationManager stopUpdatingLocation];
   [[DHWebservices sharedInstance] sendLocationWithLocation:[locations objectAtIndex:0] success:^(NSDictionary *data) {
    [self endBackgroundTask];
   } errorBlock:^(id error) {

  }];

}

I am using Firebase to send notification using postman.

this is my notification format:

{
 "to" : "fF97NwwMTvw:APA91bERqtpIxpSVXNujO_Wl1dVuPXuaJyNP2ygtVlNXxp618ykSubhQZXnh5BS3VPkqdto0lzPzJtQIzR0OIgq8oFZAuhhWfdWim5XRlxv3ut9Y1QGzu6VafNU3OqU8pJT75gg9J3-g",
 "content_available": true,
"priority": "high",
"data" : {
  "key1" : "abc",
  "key2" : 123
}
}
Manish Malviya
  • 546
  • 2
  • 15
  • 1
    A silent push will not relaunch an app that has been terminated by the user. A pushkit push will, but your app must be a voip app to use push kit. – Paulw11 Jan 15 '18 at 19:55

0 Answers0