iOS8 UILocalNotification 不工作
iOS8 UILocalNotification not working
我正在使用以下代码进行本地通知。但它不起作用。位置已成功更新并进入这些方法,但未触发通知。有什么想法吗?:
注意:当应用程序处于后台时它可以工作,但当应用程序关闭时它不工作。
- (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
如果您的应用 运行 即使在后台,您的代码也不会执行。寻找 Background modes(跟踪用户的位置部分)以获取可能的解决方案,以要求系统通过事件启动您的应用程序,即使当前它不在内存中
Wen app is closed applicationDidEnterBackground was called 所以把这个后台任务代码。和 class 本地通知。
- (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];
}
觉得对你有帮助
即使您的应用程序已从后台移除,本地通知也能正常工作。但在您的情况下,您正在侦听位置管理器事件并在委托方法内触发本地通知。一旦您终止应用程序,您的位置管理器事件将不会被触发。所以你的本地通知根本不会被触发。
当您的应用被终止时,信标监控仍会重新启动它——这就是信标监控的伟大之处!
你需要做的就是在application:didFinishLaunchingWithOptions:
方法中重新启动监控。如果您这样做,导致应用程序启动的 didEnterRegion
事件将立即传递给委托,这应该会触发通知。
Estimote 有更详细的指南:
Launching notifications in iOS when the app is killed。它使用 Estimote SDK 中的 ESTBeaconManager、ESTBeaconManagerDelegate 和 ESTBeaconRegion 类,但您可以简单地将它们替换为常规核心位置 类 以获得相同的效果。
我的情况是通知被禁用了
Settings -> Notifications -> YOUR_APP -> Allow Notifications
我正在使用以下代码进行本地通知。但它不起作用。位置已成功更新并进入这些方法,但未触发通知。有什么想法吗?:
注意:当应用程序处于后台时它可以工作,但当应用程序关闭时它不工作。
- (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
如果您的应用 运行 即使在后台,您的代码也不会执行。寻找 Background modes(跟踪用户的位置部分)以获取可能的解决方案,以要求系统通过事件启动您的应用程序,即使当前它不在内存中
Wen app is closed applicationDidEnterBackground was called 所以把这个后台任务代码。和 class 本地通知。
- (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];
}
觉得对你有帮助
即使您的应用程序已从后台移除,本地通知也能正常工作。但在您的情况下,您正在侦听位置管理器事件并在委托方法内触发本地通知。一旦您终止应用程序,您的位置管理器事件将不会被触发。所以你的本地通知根本不会被触发。
当您的应用被终止时,信标监控仍会重新启动它——这就是信标监控的伟大之处!
你需要做的就是在application:didFinishLaunchingWithOptions:
方法中重新启动监控。如果您这样做,导致应用程序启动的 didEnterRegion
事件将立即传递给委托,这应该会触发通知。
Estimote 有更详细的指南: Launching notifications in iOS when the app is killed。它使用 Estimote SDK 中的 ESTBeaconManager、ESTBeaconManagerDelegate 和 ESTBeaconRegion 类,但您可以简单地将它们替换为常规核心位置 类 以获得相同的效果。
我的情况是通知被禁用了
Settings -> Notifications -> YOUR_APP -> Allow Notifications