我正在使用以下代码进行本地通知。但它不起作用。位置已成功更新,并且已进入这些方法但未通知通知。有什么想法?:
注意:当应用程序处于后台但当应用程序关闭时无法正常工作。
- (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];
}
}
答案 0 :(得分:1)
您应该记住,只有当您的应用在后台而非前台时才会显示通知。如果你在AppDelegate的前台实现- application:didReceiveLocalNotification:
并自己手动处理通知。
<强> UPD 强>
如果您的应用即使在后台运行,您的代码也不会被执行。查找Background modes(跟踪用户的位置部分)以获取可能的解决方案,以便通过事件要求系统启动您的应用,即使它当前不在内存中
答案 1 :(得分:1)
我的情况是通知被禁用了
Settings -> Notifications -> YOUR_APP -> Allow Notifications
答案 2 :(得分:0)
文应用已关闭applicationDidEnterBackground被调用所以把这个后台任务代码。并对本地通知进行分类。
- (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];
}
我认为这对你有帮助。
答案 3 :(得分:0)
即使您的应用程序已从后台删除,本地通知也会有效。但在您的情况下,您正在侦听位置管理器事件并在委托方法中触发本地通知。杀死应用程序后,您的位置管理器事件不会被触发。因此,您的本地通知根本不会被触发。
答案 4 :(得分:0)
当您的应用程序被杀死时,信标监控仍会重新启动它 - 这对于信标监控来说是件好事!
您需要做的就是使用application:didFinishLaunchingWithOptions:
方法重新启动监控。如果您这样做,导致应用程序启动的didEnterRegion
事件将立即传递给代理人,该代理人应该触发通知。
Estimote有更详细的指南: Launching notifications in iOS when the app is killed。它使用来自Estimote SDK的ESTBeaconManager,ESTBeaconManagerDelegate和ESTBeaconRegion类,但您可以使用常规Core Location类替换它们以获得相同的效果。