1

I used this code to awake app from close to background in push notification as

UIMutableUserNotificationAction *action2;
action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode:UIUserNotificationActivationModeBackground];////UIUserNotificationActivationModeBackground
[action2 setTitle:@"ACCEPT"];
[action2 setIdentifier:NotificationActionTwoIdent];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];

Now I am hitting web service in following method //Remote Notificaiton

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler 
{
    /Call to web services.
    //show local notification in background

     UILocalNotification *localNotification = [[UILocalNotification alloc] init];
     if (localNotification == nil)
     {
         return;
     }
     else
     {
         localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
         localNotification.alertAction = nil;
         localNotification.soundName = UILocalNotificationDefaultSoundName;
         localNotification.alertBody = [NSString stringWithFormat:@"Worked=> %@",jsonArray];
         localNotification.alertAction = NSLocalizedString(@"Read Msg", nil);
         localNotification.applicationIconBadgeNumber=1;
         localNotification.repeatInterval=0;
         [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
     }
}

I enabled background fetch in app. My Questions are

  1. I am using NSURLConnetion class for network call. it works sometime but not seems to other time.
  2. Notification alert not showing in background.
  3. Do I need to use performFetchWithCompletionHandler completionHandler delegate? Any one could help me to fix this? thanks in advance.
2
  • Do you want to show notification when background task is completed? Jun 28, 2016 at 5:42
  • Please check my answer I used like this. Jun 28, 2016 at 5:49

2 Answers 2

1

I implemented like this :

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier
completionHandler:(void (^)())completionHandler {
        self.backgroundSessionCompletionHandler = completionHandler;

        //add notification
        [self presentNotification];
}

-(void)presentNotification{
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.alertBody = @"Upload Complete!";
        localNotification.alertAction = @"Background Transfer Upload!";

        //On sound
        localNotification.soundName = UILocalNotificationDefaultSoundName;

        //increase the badge number of application plus 1
        localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

        [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
1
  • @AvijitNagare Your welcome. If this answer is working then kindly accept it please. Jun 28, 2016 at 6:23
0

After your call to web servies, you should call the completion handler:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
{
    //Call to web services.
    if(completionHandler != nil)
        completionHandler();
}
3
  • Yes,i do. Can i use NSURLSession api ? Jun 28, 2016 at 6:10
  • yes you can, after your request gets a response and your work gets finished, you have to call completionHandler(), else it will keep waiting for you to tell it that your work is completed, and it wont work next time
    – Zaraki
    Jun 28, 2016 at 6:13
  • also check that your background request is not taking a lot of time.
    – Zaraki
    Jun 28, 2016 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.