简体   繁体   中英

Local Notification not working iOS 8

I want to fire local notification on particular time daily, i have wrote this code snippet but it is not receiving notification.

- (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 class

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

My Log shows didReceiveLocalNotification but notification not appearing in device :(

Where i am making mistake please help.

Thanks in advance

The notification won't appear while app is in foreground. The notification will show up only if the app totally close or in the background.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM