UILocalNotification issue with iOS9

2019-04-21 08:14发布

问题:

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 might causing the issue? I saw some posts, that there's a bug in iOS9, but I'm not sure that's the reason.

Here's a piece of code:

    NSDate *alarmDate = [date dateByAddingTimeInterval:DEFAULT_SNOOZE_DURATION * i];  
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];  
    localNotif.fireDate = alarmDate;  
    localNotif.timeZone = nil;          
    localNotif.alertBody = alertBody;  

    localNotif.hasAction = YES;  

    localNotif.applicationIconBadgeNumber = 1;  
    localNotif.soundName = UILocalNotificationDefaultSoundName;  
    localNotif.repeatInterval = NSCalendarUnitDay;  

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]  

Thanks for your help

回答1:

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


回答2:

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!



回答3:

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.



回答4:

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