iOS中的通知操作背景提取?

时间:2016-06-28 05:38:45

标签: ios objective-c apple-push-notifications

我使用此代码将推送通知中的应用程序从接近后台唤醒为

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

现在我按照以下方法点击网络服务 //远程通知

- (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委托? 任何人都可以帮我解决这个问题?提前谢谢。

2 个答案:

答案 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];
}

答案 1 :(得分:0)

在调用Web服务之后,您应该调用完成处理程序:

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