UILocalNotification issue with iOS9

前端 未结 4 1289
不思量自难忘°
不思量自难忘° 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条回答
  • 2021-02-20 15:12

    From the Local and Remote Notification Programming Guide:

    In iOS 8 and later, apps that use either local or remote notifications must register the types of notifications they intend to deliver. The system then gives the user the ability to limit the types of notifications your app displays. The system does not badge icons, display alert messages, or play alert sounds if any of these notification types are not enabled for your app, even if they are specified in the notification payload.

    As shown in the guide, the sample code that Apple shows is:

     UIUserNotificationType types = UIUserNotificationTypeBadge |
                 UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    
    UIUserNotificationSettings *mySettings =
                [UIUserNotificationSettings settingsForTypes:types categories:nil];
    
    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    

    https://stackoverflow.com/a/27376475/6219830 by : Naughty_Ottsel

    This is my code:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        UIUserNotificationType types = UIUserNotificationTypeAlert;
        UIUserNotificationSettings *mySettings =[UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    
        NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    
        localNotification.fireDate = date;
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        localNotification.alertBody = @"Finally got it!";
        localNotification.soundName = UILocalNotificationDefaultSoundName;
    
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    
        return YES;
    }
    

    I hope that help!

    0 讨论(0)
  • 2021-02-20 15:18

    Include the below code in Appdelegate.m

    if(IS_OS_8_OR_LATER)
    {
        UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge;
        UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    }
    
    0 讨论(0)
  • 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];
    }
    
    0 讨论(0)
  • 2021-02-20 15:31

    if You need it for iOS9 but under swift 3.1:

    final func registerToGetLocalNotificationsFor_iOS9(){
        let types : UIUserNotificationType = [.sound, .alert /*, .badge*/]
        let mySettings = UIUserNotificationSettings(types: types, categories: nil)
        UIApplication.shared.registerUserNotificationSettings(mySettings)
    
    }
    

    on iOS10 is deprecated.

    0 讨论(0)
提交回复
热议问题