UILocalNotification issue with iOS9

前端 未结 4 1182
不思量自难忘°
不思量自难忘° 2021-02-20 14:38

Since iOS9, local notification aren\'t working properly. Sometimes users receive the notification, sometimes they just don\'t. My notifications are repeated daily. Any idea what

4条回答
  •  梦毁少年i
    2021-02-20 15:29

    write below code in AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    // Override point for customization after application launch.
    
    [launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
    {
    
        [application registerUserNotificationSettings:[UIUserNotificationSettings
                                                       settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|
                                                       UIUserNotificationTypeSound categories:nil]];
    }
    
    return YES;
    

    }

    also write this code in AppDeleagte.m

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    {
    UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:@"Reminder"    message:notification.alertBody
                                                               delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    
    [notificationAlert show];
    
    // Set icon badge number to zero
    application.applicationIconBadgeNumber = 0;
    }
    

    write below code in your ViewController, where you wants to implement your local notification.

    forExample : Here I initialise UILocalNotification, and Its Firedate is currentTime and 5seconds Interval Time.

    After Fire localNotification it will give alert on Your Screen.

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    notification.repeatInterval = NSDayCalendarUnit;
    notification.alertBody = @"This is local notification!";
    notification.timeZone = [NSTimeZone defaultTimeZone];
    notification.soundName = UILocalNotificationDefaultSoundName;
    notification.applicationIconBadgeNumber = 10;
    
    
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }
    

提交回复
热议问题