UIlocalNotification 现在确实在没有启动后台任务的情况下出现

Posted

技术标签:

【中文标题】UIlocalNotification 现在确实在没有启动后台任务的情况下出现【英文标题】:UIlocalNotification did now appear without starting background task 【发布时间】: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];

现在的问题是:

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

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

我正在使用iPhone 3gsiPhone 4iPhone 5

【问题讨论】:

【参考方案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];

【讨论】:

以上是关于UIlocalNotification 现在确实在没有启动后台任务的情况下出现的主要内容,如果未能解决你的问题,请参考以下文章

自定义 iOS UILocalNotification 声音不播放(可能与 Xcode 更新有关)

UILocalNotification 在一段时间后重复自己

为啥我的 UILocalNotification 没有播放任何声音?

UILocalNotification - 在每个时区的早上 6:00 触发

UILocalNotification 和 NSTimeZone,出现错误的时区

UILocalNotification - 应用程序未运行时如何处理?