iOS8 UILocalNotification不工作
我使用以下代码进行本地通知。但它不起作用。位置已成功更新,并已进入这些方法,但未触发通知。有什么想法吗 注意:当应用程序处于后台时,它会工作,但当应用程序关闭时,它不会工作iOS8 UILocalNotification不工作,ios,objective-c,ios8,uilocalnotification,ibeacon,Ios,Objective C,Ios8,Uilocalnotification,Ibeacon,我使用以下代码进行本地通知。但它不起作用。位置已成功更新,并已进入这些方法,但未触发通知。有什么想法吗 注意:当应用程序处于后台时,它会工作,但当应用程序关闭时,它不会工作 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([UIApplication instancesRespondToSelector:@s
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([UIApplication instancesRespondToSelector:@selector(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) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
if ([region isKindOfClass:[CLBeaconRegion class]]) {
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"You are checked in";
notification.soundName = @"Default";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
}
你应该记住,只有当你的应用程序在后台时才会出现通知,而不是在前台。如果您在前台,请实现AppDelegate的
-application:didReceiveLocalNotification:
,并自己手动处理通知
UPD
如果你的应用程序甚至没有在后台运行,你的代码将不会被执行。查找(跟踪用户位置部分)可能的解决方案,以便要求系统通过事件启动您的应用程序,即使当前它不在内存中Wen应用程序已关闭应用程序标识背景已调用,因此请输入此后台任务代码。并对本地通知进行分类
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
//// just u call the method want ever u want example
[self notification];
}
- (void)notification
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"You are checked in";
notification.soundName = @"Default";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
我认为这对你很有帮助。即使你的应用程序被从后台删除,本地通知也能起作用。但在您的情况下,您正在侦听location manager事件并在委托方法内触发本地通知。一旦终止应用程序,将不会触发location manager事件。因此,您的本地通知根本不会被触发。当您的应用程序被终止时,信标监控仍将重新启动它-这是信标监控的一大优点 您只需在
应用程序:didFinishLaunchingWithOptions:
方法中重新启动监控。如果您这样做,导致应用程序启动的didEnterRegion
事件将立即传递给代理,代理将触发通知
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
//// just u call the method want ever u want example
[self notification];
}
- (void)notification
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"You are checked in";
notification.soundName = @"Default";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
Estimote有一个更详细的指南:
. 它使用EstMote SDK中的ESTBeaconManager、ESTBeaconManagerDelegate和ESTBeaconRegion类,但您可以简单地将它们替换为常规的核心位置类,以实现相同的效果。我的情况是从
设置->通知->您的应用程序->允许通知
是的,您是对的。请看这篇文章。当应用程序关闭或终止时,我需要得到通知。@Idrees我已经编辑了关于您评论的答案是的,我已经在后台模式下检查了位置更新,但它仍然不起作用