我有一个调用scheduleLocalNotification
的应用,但是当我将其安装到 / Applications 而不是 / var / mobile / Applications 时它不起作用:
- (void) doNotify
{
// this doesn't work when app is in /Applications but does in /var/mobile/Applications
UILocalNotification * theNotification = [[UILocalNotification alloc] init];
theNotification.alertBody = @"Finished processing.";
theNotification.alertAction = @"Ok";
theNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
[[UIApplication sharedApplication] scheduleLocalNotification:theNotification];
NSLog(@"notification scheduled: %@", theNotification);
}
我尝试了presentLocalNotification
,以防它是时间问题。
我在app delegate中实现了didReceiveLocalNotification
以查看是否正在调用它,但事实并非如此,只有当应用程序处于前台时才会调用它。
如果我将应用程序放回 / var / mobile / Applications ,它可以正常工作。 我正在使用Xcode 4.2.1并在iPhone 4S和iPod Touch 4g上运行iOS 5.1.1
编辑:应用可以在后台运行,因为它是音乐应用
答案 0 :(得分:0)
我刚才遇到了同样的问题。我已经通过官方文件解决了这个问题。
事实上,您应该注册通知以获取使用通知的权限:
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication shareApplication] registerUserNotificationSettings: settings];
答案 1 :(得分:0)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeAlert| UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];
}
[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
NSLog(@"startLocalNotification");
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
notification.alertBody = @"notification Message ";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
//notification.applicationIconBadgeNumber = 10;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}