2016-06-28 35 views
1

我用这个代码从接近背景清醒应用在推送通知的iOS中的通知操作后台获取?

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

现在我打的Web服务在下面的方法 //远程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]; 
    } 
} 

我启用后台中取应用程序。我的问题是

  1. 我正在使用NSURLConnetion类进行网络调用。它在某个时候有效,但似乎没有其他时间。
  2. 通知警报不会在后台显示。
  3. 我是否需要使用performFetchWithCompletionHandler completionHandler委托? 任何人都可以帮助我解决这个问题?提前致谢。
+0

你想显示通知,当后台任务完成? –

+0

请检查我的答案,我用这个。 –

回答

1

我实现这样的:

- (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]; 
} 
+0

@AvijitNagare您的欢迎。如果这个答案正在工作,请接受它。 –

0

您的来电网页servies后,你应该调用完成处理程序:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler 
{ 
    //Call to web services. 
    if(completionHandler != nil) 
     completionHandler(); 
} 
+0

是的,我这样做。我可以使用NSURLSession API吗? –

+0

是的,你可以,在你的请求得到响应并且你的工作完成之后,你必须调用completionHandler(),否则它会一直等着你告诉它你的工作已经完成,并且下次不会工作 – Zaraki

+0

也会检查您的背景请求没有花费很多时间。 – Zaraki