3

我有一个调用的应用程序,但是当我将它安装到/Applications而不是/var/mobile/ApplicationsscheduleLocalNotification时它不起作用:

- (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了以防万一这是时间问题。

didReceiveLocalNotification在应用程序委托中实现以查看是否被调用,但不是,它仅在应用程序处于前台时才被调用,就像它应该那样。

如果我将应用程序放回/var/mobile/Applications,它应该可以正常工作。我在 iPhone 4S 和 iPod Touch 4g 上使用 Xcode 4.2.1 并运行 iOS 5.1.1

编辑:应用程序可以在后台运行,因为它是一个音乐应用程序

4

2 回答 2

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];


}
于 2016-04-07T04:35:12.000 回答
0

我刚才遇到了同样的问题。而且我已经通过官方文档解决了这个问题。

实际上,您应该注册通知以访问使用通知:

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication shareApplication] registerUserNotificationSettings: settings];
于 2016-04-07T03:05:43.743 回答