【问题标题】:UIlocalNotification did now appear without starting background taskUIlocalNotification 现在确实在没有启动后台任务的情况下出现
【发布时间】:2013-10-10 06:15:00
【问题描述】:

我有一个 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 通知以显示带有两个按钮的警报 CancelReceive 在一秒钟后触发。

现在的问题是:

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

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication* app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{        
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];
}

但 10 分钟后后台任务停止。然后本地通知不会出现给用户,尽管它触发(触发后打印日志)

我确实修改了applicationDidEnterBackground 函数如下重新启动后台任务以长期运行。但不能。

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

现在的问题是:

  1. 当应用程序处于后台时,有什么方法可以触发本地通知并向用户显示?

  2. 如果后台任务是强制性的,如何在 10 分钟后向用户显示本地通知?

我正在使用iPhone 3gsiPhone 4iPhone 5

【问题讨论】:

    标签: iphone ios objective-c uilocalnotification


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多