OGeek|极客世界-中国程序员成长平台

标题: iOS8 UILocalNotification 不起作用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:30
标题: iOS8 UILocalNotification 不起作用

我正在使用以下代码进行本地通知。但它不起作用。位置已成功更新并进入这些方法,但未触发通知。有什么想法吗?:

注意:当应用程序在后台时它可以工作,但在应用程序关闭时不工作。

- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
    if ([UIApplication instancesRespondToSelectorselector(registerUserNotificationSettings]){
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    // Override point for customization after application launch.
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    return YES;
}

-(void) locationManagerCLLocationManager *)manager didEnterRegionCLRegion *)region {
    if ([region isKindOfClass:[CLBeaconRegion class]]) {
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.alertBody = @"You are checked in";
        notification.soundName = @"Default";
        [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
    }
}



Best Answer-推荐答案


您应该记住,通知仅在您的应用处于后台而不是前台时出现。如果您在前台,请执行 AppDelegate 的 - application:didReceiveLocalNotification: 并自行手动处理通知。

UPD

如果您的应用即使在后台也没有运行,您的代码将不会被执行。寻找 Background modes (跟踪用户的位置部分)寻找可能的解决方案,以通过事件要求系统启动您的应用程序,即使当前它不在内存中

关于iOS8 UILocalNotification 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29004815/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4