我在iOS 8上使用LocalNotification,因此即使在真实设备中,声音也无法正常工作,这是我的代码

在AppDelegate.m中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

     if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
{
         [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
        [self.window makeKeyAndVisible];

      return YES;
}


在ViewController.m中

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody = @"Time to get up!";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.timeZone=[NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification.applicationIconBadgeNumber=1;

最佳答案

您必须在安排通知之前设置徽章

localNotification.applicationIconBadgeNumber=1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];


对于声音,您的代码是正确的。检查您的设备声音。从设置>声音。

10-06 01:42