本地通知无法使用iOS 8

时间:2015-04-13 07:59:39

标签: ios iphone ipad notifications uilocalnotification

我想每天在特定时间点击本地通知,我已经写了这段代码,但是没有收到通知。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 //  running on iOS8
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
        [application registerUserNotificationSettings:settings];
    }
    else // iOS 7 or earlier
    {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
    }

}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    NSLog(@"didReceiveLocalNotification----");
    application.applicationIconBadgeNumber = 0;

}

// ViewController类

- (void)viewDidLoad
{
    [super viewDidLoad];

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

    [dateComponents setHour:7];
    [dateComponents setMinute:59];
    NSDate *currentDate = [NSDate date]; //2015-04-13 07:56:09 +0000
    NSDate *fireDate = nil;
    fireDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents
                                          toDate:currentDate
                                         options:0];

    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    localNotification.alertBody = @"Notiication success....";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;




    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

我的日志显示didReceiveLocalNotification,但通知未显示在设备中:(

我在哪里犯错误请帮忙。

提前致谢

1 个答案:

答案 0 :(得分:0)

当应用处于前台时,通知不会显示。只有在应用程序完全关闭或处于后台时,通知才会显示。