Search code examples
iosuilocalnotificationreminders

Reminder is not working


I want to remind the user based on time,but cannot achieve using this code,please give me any solution.This is my code:

NSDate *pickerDate = [self.StartDate date];

    UIApplication* app = [UIApplication sharedApplication];
    UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];

    NSDate *date1=[pickerDate dateByAddingTimeInterval:60];
    notifyAlarm.fireDate = date1;
    notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
    //notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
    notifyAlarm.repeatInterval =NSCalendarUnitWeekday;
    notifyAlarm.soundName =UILocalNotificationDefaultSoundName;
    notifyAlarm.alertBody =self.EventText.text;
    //notifyAlarm.alertLaunchImage=@"in.png";
    [app scheduleLocalNotification:notifyAlarm];
    [self dismissViewControllerAnimated:YES completion:nil];

Solution

  • Write this code in didFinishLaunch method in appdelegate.m

     if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
                UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                                UIUserNotificationTypeBadge |
                                                                UIUserNotificationTypeSound);
                UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                                         categories:nil];
                [application registerUserNotificationSettings:settings];
                [application registerForRemoteNotifications];
            } else {
    
                [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                                 UIRemoteNotificationTypeAlert |
                                                                 UIRemoteNotificationTypeSound)];
            }