UILocalNotification 不会在 didEnterRegion 中触发

UILocalNotification doesn't fire in didEnterRegion

我已经实施了

didEnterRegion

方法,它在后台工作,例如,如果我显示一个警报视图......即使应用程序在后台(当我将应用程序置于前台时,alertViews 显示) 我使用 alertview 只是为了在后台测试 didEnterRegion 方法并且它有效......但是现在我m trying to fire an UILocalNotification in didEnterRegion but the UILocalNotification is not firing. Theres 我的代码:

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    int iRegiao = [region.identifier intValue];
    Location *ocorrencia = (Location *)[self.locations.objects objectAtIndex:iRegiao];
  //  NSLog(@"Voce está proximo da %@", ocorrencia.name);
    // UIApplication *app = [UIApplication sharedApplication];

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

    notification.fireDate = [NSDate date];// Now here you can manage the fire time.
    notification.timeZone = [NSTimeZone defaultTimeZone];
    notification.alertBody = ocorrencia.name;
    notification.alertAction = ocorrencia.name;
    notification.soundName = UILocalNotificationDefaultSoundName;
    notification.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

您是否尝试在 UIApplication 上使用 - (void)presentLocalNotificationNow:(UILocalNotification *)notification? (忽略 fireDate

您的目标是 iOS 8?如果是这样,您需要请求本地通知的权限。

试试这个:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
   [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge categories:nil]];
}

如果您还支持 iOS 7 或更低版本,则需要进行 respondsToSelector: 检查。