UILocalNotification issue with iOS9

前端 未结 4 1258
不思量自难忘°
不思量自难忘° 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!

提交回复
热议问题