Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIlocalNotification现在没有启动后台任务就出现了_Iphone_Ios_Objective C_Uilocalnotification - Fatal编程技术网

Iphone UIlocalNotification现在没有启动后台任务就出现了

Iphone UIlocalNotification现在没有启动后台任务就出现了,iphone,ios,objective-c,uilocalnotification,Iphone,Ios,Objective C,Uilocalnotification,我有一个voip应用程序 我在我的应用程序中使用UILocalNotification,在应用程序处于后台时,为传入呼叫发出警报通知 当应用程序在后台时,在收到传入呼叫后,调用以下函数 -(void)showLocalNotification:(NSNotification *)notification { NSDictionary *dic = notification.userInfo; NSString *msg = [dic objectForKey:@"msg"];

我有一个voip应用程序

我在我的应用程序中使用
UILocalNotification
,在应用程序处于后台时,为传入呼叫发出警报通知

当应用程序在后台时,在收到传入呼叫后,调用以下函数

-(void)showLocalNotification:(NSNotification *)notification {

    NSDictionary *dic = notification.userInfo;
    NSString *msg = [dic objectForKey:@"msg"];
    NSString *sound = [dic objectForKey:@"sound"];

    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    UILocalNotification *_localNotification = [[UILocalNotification alloc]init];

    //setting the fire dat of the local notification
    _localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];

    //setting the time zone
    _localNotification.timeZone = [NSTimeZone defaultTimeZone];

    //setting the message to display
    _localNotification.alertBody = msg;

    //default notification sound
    if ([sound length]==0) {
        _localNotification.soundName = UILocalNotificationDefaultSoundName; 
    } else {
        _localNotification.alertAction = NSLocalizedString(@"Receive", nil);
        _localNotification.soundName = @"myringtone.aif";
    }
    //displaying the badge number
    _localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;

    //schedule a notification at its specified time with the help of the app delegate
    [[UIApplication sharedApplication]scheduleLocalNotification:_localNotification];

}
此功能设置scheduleLocal通知,以显示带有两个按钮的警报
Cancel
Receive
,这两个按钮在一秒钟后触发

现在的问题是:

仅当后台任务按以下方式启动时,此本地通知才会显示给用户

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication* app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{        
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];
}
但10分钟后,后台任务停止。然后,虽然本地通知触发,但用户不会看到它(触发后会打印日志)

我确实修改了
applicationidenterbackground
函数,如下所示,以重新启动后台任务以进行长时间运行。但是不能

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    expirationHandler = ^{
        [app endBackgroundTask:self.bgTask];
        self.bgTask = UIBackgroundTaskInvalid;
        self.bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
    }
   self.bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
}
现在的问题是:

  • 当应用程序处于后台时,是否有任何方式可以触发本地通知并显示给用户

  • 若后台任务是强制性的,那个么在10分钟后显示本地通知以显示给用户怎么可能是长期运行的呢

  • 我正在使用
    iphone3gs
    iphone4
    iphone5

    试试这个:

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
    
        __block UIBackgroundTaskIdentifier bgTask ;
        UIApplication  *app = [UIApplication sharedApplication];
        bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
            [app endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        }];
        pollingTimer2 = [NSTimer scheduledTimerWithTimeInterval:4.0f target:self  selector:@selector(process)  userInfo:nil repeats:YES];
    }
    -(void) process
    {
       [self didLocalNotification];
    
    }
    
    -(void)didLocalNotification
    {
    
        [[UIApplication sharedApplication]cancelAllLocalNotifications];
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        if (localNotification == nil)
        {
            return;
        }
    
        NSLog(@"calling didLocalNotification");
        localNotification.applicationIconBadgeNumber =0;
        localNotification.alertBody =@"Message!";
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    }